How to Install Library in Python?
Modules in Python allow programmers to utilize other people’s code independently. This can help programmers save a lot of development work because they don’t have to start from scratch every time. The programming language Python has tons of modules that can help programmers save time when programming.
Python modules can be installed in 2 ways:
- One is system-wide
- Virtual environment
In this article, we will discuss how to install library in Python.
Requirements for Installing Libraries
Here are a few points we need to keep in mind while installing libraries in Python.
1) Make Sure Python is Installed: Before proceeding, programmers must ensure that they must have Python installed in their systems as well as that the required version of the programming language is available via the command line (known as Cmd).
Programmers can easily test this by typing:
- Command for macOS/Unix users:
- Command for Windows users:
On using the commands above the output would be as follows.
Output: Programmers should get something like Python 3.x, where x is the latest version of Python as the output. If the actual output is not similar as stated above, it means you don't have Python. Users can download the most recent 3.x version of Python from the official site of Python: python.org.
If you are new to Python, you would be getting the output as stated below.
The above-discussed commands are meant to be executed in a shell (which can also be known as a console or terminal). This is the reason you might be getting the error stated above.
2) Check to see if pip is installed:
Programmers must ensure that they must have pip installed in their systems. Programmers can easily test this by typing:
- Command for macOS/Unix users :
- Command for Windows users:
If Python is already installed by Programmers directly, with the aid of a Homebrew, or through, the python.org installer, programmers should have pip installed in their systems. Configuring pip/setuptools/wheel using Linux Package Managers might be required if programmers have installed pip using their operating system's package manager.
Creating Virtual Environments
Python applications frequently make utilization of modules as well as packages that are not included in the standard library. These Python applications might also require a certain edition of a library since the Python application requires that a particular issue be solved or because the application was created with the help of an old version of the library's interface.
We can conclude that a single Python installation might not have been able to suit the needs of all applications. Let's suppose if application X requires v1.0 of a specific module while application Y requires v2.0, then the requirements are said to be incompatible, hence installing any of the two versions 1.0 or 2.0 will prevent one program from running.
The module named venv is used to construct as well as maintain virtual environments. The module venv is generally used to download the most latest Python version that is available. If there are numerous editions of Python installed on the machine, then it can be chosen one by one by executing python3 or whatever version you wish to.
Now, how to create this virtual environment? To construct a virtual environment, the first step is to choose a directory and the second step is to execute the venv module in the form of a script also with the path of the directory as follows:
If the tutorial-env directory does not already exist, then it would be created, as will directories within it including a duplicate of the Python interpreter along with numerous supporting files.
Now, the virtual environment has been created and all we need is to activate it.
To activate the virtual environment on Windows, execute the command given below:
To activate the virtual environment on Unix or macOS, execute the command given below:
This is how we can easily create Virtual environments in Python.
How to Install Libraries in Python Using Pip?
In this part of the article, let’s discuss how to install the library in Python using the pip command.
If you are a Python Programmer who is using Windows, Python packages can be installed by launching the Command Prompt in Windows as well as executing the following command:
Note: The above-shown solution will only operate if Python was already in your Windows path.
This is how to install the library in Python using the pip command.
How to Install Libraries in Python without Using Pip?
In this part of the article, let’s discuss how to install the library in Python without using the pip command. In the programming language Python, the pip command is the most commonly used method for obtaining external libraries in the system. There is, however, an alternative way to directly install Python libraries instead of making use of the pip command.
The procedure for manually installing any library in a system is outlined below.
- The first step is to download the files. Locate the package you download and install in https://pypi.org/ website. Select the download files button which is present on the menu icon.
- If the obtained files are zipped, use any unzipping software to unzip them.
- By using the command cd, modify the current working directory to the file which contains Setup.py.
- Review the user manual carefully as well as follow them exactly.
- After completing Step 3, the command given below needs to be typed:
How to Install Libraries in Python Using a Script?
While working with Python and utilizing libraries you don't know or if using a brand new computer, installing all the libraries individually is a tedious task. Every time, you must determine the names of the libraries as well as install it one at a time. We will discuss how to install libraries in Python using a script.
Installing pipreqs will be the first step while trying to install libraries in Python using a script.
Execute the command given below to set up the pipreqs library on your computer.
The next step will be navigating to the Python script's directory. In that specific directory, we need to generate a requirement file which can be done using this code.
Execute the following command to download all the libraries needed to run the application.
Now when the code is executed it will be successful.
How to Install Libraries in Python Manually?
The majority of Python packages currently accept the pip command. If there's any case where there is an incompatible kit, you must install it manually.
Download the package as well as unzip it to a local directory to activate it. If the kit comes with its own set of installation guides, read all the instructions; but if the package is missing, then you can make use of the following command to install it manually:
How to Uninstall Libraries in Python Using Pip?
We have already discussed how to install Python library, let’s discuss how we can uninstall libraries in Python. If you are a Python programmer using Windows, you can uninstall Python packages by opening the Command Prompt as well as running the following command:
Note: The technique mentioned above will only work if Python is already in your Windows path.
To uninstall Libraries in Python using pip for Unix/macOS the command given below can be used:
Learn More about Python Modules and Libraries
If you want to learn more about various libraries in Python and how to install them refer to the links given below:
- If you want to know about pip, refer to this article.
- To learn more about Python modules, Click here.
- Refer to this link if you are a Linux user who wants to learn how to install Python.
Conclusion (in points)
We have discussed how to install libraries in Python in this article.
- We now know how to install Python library. There are various ways by which we can easily install libraries in Python.
- Any Python library can be installed manually in just one step using the command: python3 <FILE_NAME>.py install.
- The command pip install package_name is used in Python to install libraries using scripts.
- Programmers can also use the pip command to uninstall Python libraries.