3 Ways to Install Python Package

Deepanshu Bhalla Add Comment

Python is the most popular programming language for a variety of tasks. The beauty of this language is that it is open-source which makes it freely available and benefits from a very active community of developers around the world. Python developers share their solutions as packages or modules with other python users. This tutorial explains various ways to install a python package.


Method 1 : Using pip

If you are using Jupyter Notebook or IPython Console, make sure to use ! before pip when you enter the command below. Otherwise it would return syntax error.

!pip install package_name

The ! prefix tells Python to run a shell command.

Syntax Error : Installing Package using PIP

Some users face error "SyntaxError: invalid syntax" in installing packages. To workaround this issue, run the command line below in command prompt -

python -m pip install package-name

python -m pip tells python to import a module for you then run it as a script.

Install Specific Versions of Python Package
python -m pip install Packagename==1.3 # specific version
python -m pip install "Packagename>=1.3" # version greater than or equal to 1.3

Method 2 : Using conda

Anaconda is the data science platform which comes with pre-installed popular python packages and powerful IDE (Spyder) to ease writing of python scripts.

If Anaconda is installed on your system (laptop), click on Anaconda Prompt as shown in the image below.
Anaconda Prompt

To install a python package or module, enter the code below in Anaconda Prompt -

conda install package-name

Note : If you get an error that you don't have necessary permission to install package or write permissions to the target environment. To fix this, you need to right click on Anaconda Prompt and select Run as administrator. Then run the above install command.

Method 3 : Using Command Prompt/Terminal

  1. Type "Command Prompt" in the search bar on Windows OS. For admin rights, right-click and choose "Run as administrator". In macOS, open "Terminal" from Utilities. In Linux OS, search for "terminal" in the applications menu.

    Prompt Screen
  2. Search for folder named Scripts where pip applications are stored. Pip : Scripts Folder
  3. In command prompt, type cd <file location of Scripts folder> cd refers to change directory. For example, folder location isC:\Users\DELL\Python37\Scripts so you need to enter the following line in command prompt :
    cd C:\Users\DELL\Python37\Scripts
    Change Directory
  4. Type pip install package-name Install Package via PIP command prompt
How to load or import package or module

Once package is installed, next step is to load the package. There are several ways to load package or module in Python :

1. import math loads the module math. Then you can use any function defined in math module using math.function. Refer the example below -

import math
math.sqrt(4)

2.import math as m imports the math module under the alias m.

import math as m
m.sqrt(4)

3. from math import * loads the module math. Now we don't need to specify the module to use functions of this module.

from math import *
sqrt(4)

4. from math import sqrt, cos imports the selected functions of the module math.

Other Useful Commands
Description Command
To uninstall a package pip uninstall package
To upgrade a package pip install --upgrade package
To search a package pip search "package-name"
To check all the installed packages pip list
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "3 Ways to Install Python Package"
Next → ← Prev