Python is known to be a high-level, interpreted, and general-purpose programing language.
Its core design philosophy allows code readability due to its well-structured indentation. Its object-oriented approach and language construct significantly assist programmers to write logical code with high code readability and proper indentation. These features allow Python to be used across the globe for a wide range of high and low-level projects.
Amongst the above general features, one of the salient features that make Python particularly popular is the availability of installing third-party Python packages based on your own requirements. This allows the programmer to modify distributions, environment, and runtime as per the needs.
Generally, most of the Python packages that are available online are now designed to be compatible with the well-known pip package manager. However, if you wish to install a package that is not compatible with pip, then in this article, we will discuss brief steps of how you can overcome this constraint and install your desired package manually step-by-step.
What you’ll need
The first and foremost important step is to figure out the installation requirements of the package you are trying to install. You can check these requirements via the readme.txt file available in the .zip file of the package you downloaded and extracted. For example, if you want to download scrapy 2.5.0, you have to make sure that you have the same version of Python with which scrapy is compatible.
- Python 3.6+
- Works on Linux, Windows, macOS, BSD
To check the version of python installed on your machine, use the following command:
In case if you find any hurdles in downloading python packages or want to know how to install the most latest version of python then check our article How To Download Python Packages?
Secondly, make sure all the necessary files needed for installing packages are in the folder where you have extracted the .zip file. To ensure this, extract the .zip file directly to your directory and avoid extracting it to some other location and then moving to your directory.
Common Mistake while Installing a Package with pip/easy_install
Pip is the PyPA (Python Packaging Authority) recommended tool for installing almost all of the available Python packages. So before proceeding to install a package manually, make sure you do not make the following mistake while trying to install the required package with pip.
The problem with pip is that if you try to install a package without root access, the pip tool will fail to add the package and will not give you any hint of installing the package without root. So in order to overcome this root access constraint, you should instruct the pip to install the package to your HOME folder by adding the
--user option as written below.
pip install --user <package>
Or, if you have Python3,
pip3 install --user <package>
One of the least possibilities of the package not getting installed via pip is using the older version of pip with respect to the package requirements, so to be sure, try the following to install the package again.
pip install --upgrade pip
Alternatively, you can also run the following command to check the version of your pip and then can compare it with the requirements of the desired package.
Similarly, easy_install will also fail to install a package without root access. However, unlike pip, it will hint at the possibility of installing the package without root access. To install a package without root access to your HOME folder, you can use the
--user option as follows.
easy_install --user <package>
Packages that Cannot Be Installed with Pip
If you tried the above steps and still could not install your required package, follow the step-by-step procedure below.
- Download the package. (Ignore if you already downloaded while trying with the above installation cases.)
- Unzip it and extract it into the local directory.
- If the package includes its own set of instructions for installation, then it’s good to directly follow them step-by-step. However, if there are no instructions, the most commonly used way of installing a package manually is to implement setup.py.
- Next up, type the following and hit run.
python setup.py install
Note: For the above step, depending upon your operating system, you may need administrative rights for running it. For instance, in an Ubuntu-based operating system, you would need to say the following to overcome the administrative privileges barrier.
sudo python setup.py install
Similarly, in a Windows-based system, you can overcome this barrier by simply using a
--user flag. In this way, you can install your package only for the current user.
Installing Python Packages Manually
Following are a few steps that can eventually lead you to install Python packages manually if you are unable to download them from the package manager. Take a look!
Installing Packages Locally from Source
Most of the available Python packages provide users the option of installing them directly from the source code. That is why most of the packages you download include a setup.py file. Similar to pip and easy_install, the use of an additional
--user flag is sufficient for setup.py to install the package directly to your HOME folder. You can do this by using the command below.
python setup.py install --user
Installing Packages Step-by-Step Using setup.py
setup.py is the build script for the packages that are built with setuptools. To install a package that includes setup.py, open the command window and do the following.
- Navigate into the root directory where setup.py is located.
- Run the following command.
python setup.py install
All the packages installed using setup.py have their own build requirements that programmers should adhere to. But not necessarily all requirements should be taken care of, some of them are optional as well.
Ensure that an updated version of setuptools is installed on your system.
python –m pip install –-upgrade setuptools
To know the minimum package requirements of your desired package, include the following in your setup.py:
install_requires=[''], #optional keyword
The install_requires is a setuptools keyword that is used to specify minimum package requirements.
Complete package build requirements for package installation are specified by PyPA (Python Packaging Authority) in the Sample Project.
The Sample Project
The sample project is a general template package outlined by the PyPA with a setup.py file for the manual installation of a package. The file is annotated with comments to assist in customizing the script and for the overall package build environment.
A setup.py Example
import setuptools with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name="", # replace with your username, version="1.0.0", author="", author_email="<[email protected]>", description="", long_description=long_description, long_description_content_type="text/markdown", url="<https://github.com/authorname/templatepackage>", packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent" ], python_requires='>=3.6' )
There you go! You can download any Python package and install it manually for whatever project you are developing. Learning how to utilize already built packages is the key to develop your project faster, as it saves plenty of your time and reduces your efforts.
We hope you have found this article useful. Feel free to share your thoughts and feedback in the comments section below.
And as always, happy coding!