How to install a Python Module?


A file containing Python definitions and statements is referred to as a module. A module is a file that contains Python code; an “Program.py” file would be a module with the name “Program”. We utilise modules to divide complicated programs into smaller, more manageable pieces. Code reuse is also possible with modules. Instead of copying their definitions into several programs, we can define our most frequently used functions in a module and import it.

Installing Python modules in Windows

The Python package manager (pip) allows for the installation of modules and packages. Open a terminal and use the pip command to install a module across the entire system.

Using PIP in Python

PIP is a package manager for Python modules or packages. PIP comes inbuilt in Python versions 3.4 and beyond.

Example

The newest version of a module and any dependencies will be installed using the following command from the Python Package Index −

C:\Users\Lenovo>pip install hashlib

Output

Following is the output after installing any modules −

Collecting hashlib
   Downloading hashlib-20081119.zip (42 kB)
      ---------------------------------------- 42.3/42.3 KB 2.1 MB/s eta 0:00:00
   Preparing metadata (setup.py) ... error
   error: subprocess-exited-with-error

   × python setup.py egg_info did not run successfully.
   │ exit code: 1
   ╰─> [7 lines of output]
      Traceback (most recent call last):
         File "<string>", line 2, in <module>
         File "<pip-setuptools-caller>", line 34, in <module>
         File "C:\Users\Lenovo\AppData\Local\Temp\pip-install-nhi53g8x\hashlib_9d646b53cf6c4df592a92567c02655dc\setup.py", line 68
            print "unknown OS, please update setup.py"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
         [end of output]
   note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.

You should consider upgrading via the 'C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python.exe -m pip install --upgrade pip' command.

Note − That will automatically install a Python module. Typically, instead of installing modules on your entire computer, you utilise a virtual environment, or venv.

You need pip set up in order for this to function. The installation procedure is platform-dependent.

Python 3.4 or newer is required because pip is included in that version.

Example

Run the following command if pip is not accessible and you are using Python 3.4 or later −

C:\Users\Lenovo>py -3 -m ensurepip

Output

Following is an output of the above code

Looking in links: c:\Users\Lenovo\AppData\Local\Temp\tmpvdjjyjwx
Requirement already satisfied: setuptools in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (58.1.0)
Requirement already satisfied: pip in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (22.0.4)

Checking PIP version

Locate the script directory for Python on your command line, and then type the following to verify your version −

C:\Users\Lenovo>python --version

The output comes as follows −

Python 3.10.5

Note

You can use pip to install python packages. For example, to install the latest version of "SomeProject" −

$ pip install 'SomeProject'

To install a specific version use the following command

$ pip install 'SomeProject==1.4'

To install greater than or equal to one version and less than another use the following command −

$ pip install 'SomeProject>=1,<2'

Using Conda in Python

Running on Windows, macOS, Linux, and z/OS, Conda is an open source package management and environment management system. Conda installs, runs, and updates packages as well as the dependencies on them quickly.

On your own computer, Conda makes creating, saving, loading, and switching between environments simple. It was designed to package and distribute Python programs, but it can do the same for any language's software.

You can search and install packages with the help of Conda as a package management. Conda is an environment manager, so you do not need to switch to another one if you require a package that needs a different version of Python.

Searching Packages

For the later steps, use an Anaconda Prompt or the terminal to check whether a certain package, like “hiredis”, is accessible for installation −

(base) C:\Users\Lenovo>conda search hiredis

Output

Following runs as an output for the above command −

Loading channels: done
# Name         Version        Build       Channel
hiredis        1.1.0    py310h2bbff1b_1   pkgs/main
hiredis        1.1.0    py36h2bbff1b_1    pkgs/main
hiredis        1.1.0    py37h2bbff1b_1    pkgs/main
hiredis        1.1.0    py38h2bbff1b_1    pkgs/main
hiredis        1.1.0    py39h2bbff1b_1    pkgs/main
hiredis        2.0.0    py310h2bbff1b_0   pkgs/main
hiredis        2.0.0    py37h2bbff1b_0    pkgs/main
hiredis        2.0.0    py38h2bbff1b_0    pkgs/main
hiredis        2.0.0    py39h2bbff1b_0    pkgs/main

Installing Packages

Use an Anaconda Prompt or the terminal to install a certain package, like “hiredis” −

(base) C:\Users\Lenovo>conda install hiredis

Output

Following runs as an output for the above command −

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##
   environment location: C:\Users\Lenovo\anaconda3
   
   added / updated specs:
   - hiredis
The following packages will be downloaded:
   package              |   build
------------------------|-----------------
conda-4.14.0            | py39haa95532_0   937 KB
hiredis-2.0.0           | py39h2bbff1b_0   23 KB
----------------------------------------------------------
                                      Total:   959 KB
The following NEW packages will be INSTALLED:

hiredis     pkgs/main/win-64::hiredis-2.0.0-py39h2bbff1b_0

The following packages will be UPDATED:

conda        4.12.0-py39haa95532_0 --> 4.14.0-py39haa95532_0
Proceed ([y]/n)? y
Downloading and Extracting Packages

hiredis-2.0.0        | 23 KB  | ############################################################################ | 100%

conda-4.14.0         | 937 KB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Updated on: 23-Nov-2022

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements