How to Install Python Dependencies Locally in a Project

How to Convert PNG to TIFF in Python?

Python is an advanced programming language used across various disciplines—going from web developer to data researchers and beyond. What makes Python especially famous is the accessibility of outside libraries and all required dependencies to quickly build and deploy your projects. As one moves past the basic scripting to the more mind-boggling code, they dive themselves into plenty of conditions, which require utilization of third-party packages and their dependencies.

In this brief article, we will discuss why Python dependencies are important and how they can be installed locally in a project.

 

Why Are Python Dependencies Important?

Any Python application dependency is a library other than your project code that is required to create and run your application. For any particular application, there could be numerous dependencies that are required to run your project.

Python applications are built upon the work done by numerous open-source developers. Application dependencies incorporate web systems as well as libraries for scratching, parsing, processing, visualizations, simulations, and numerous different errands. Python’s environment promotes discovery, retrieval, and installation. Thus, applications are easier for coders to build.

 

How To Download Python Dependencies Using Pip

The prescribed method to install Python library dependencies is with using the pip command when a virtualenv is activated.

Pip and virtualenv operate together and have integral responsibilities. Pip downloads and installs application dependencies from the PyPi repository. Pip is Python’s package managing tool, which provides fundamental features for installing and maintaining Python packages and their dependencies. However, pip does not flag dependency conflicts and automatically install different versions of a dependency in a project which then possibly results in multiple errors.

But lately, pip has been integrated with higher-level tools, such as Pipenv and others, which utilize Pip’s command-line interface (CLI) to assist in performing improved dependency management within virtual environments.

 

How To Install Latest Version Of Python Dependencies Locally In A Project

Step 1: To install virtualenv using pip.

pip install virtualenv

 

Step 2: Then create a virtual environment in your project.

virtualenv test

This will create a local directory called test. You can name the directory whatever you wish to. This directory will be a mirror of your all global Python installations. Within the test/ there will be a directory named lib, which will contain Python and will store all your dependencies.

 

Step 3: Next, you have to activate your virtual environment.

source test/bin/activate

 

Step 4:

Now there are two ways of proceeding with installing required dependencies in your environment, which depends upon if you require specific versions of any of your dependency or not.

In case dependency versions are not a concern for your project, then simply use the following.

For Python 2:

pip install --user <package_name>

For Python 3:

pip3 install --user <package_name>

 

By the above command, pip will automatically install all the latest versions of dependencies of the package you installed. If you would like to read in detail the steps of how to install Python packages manually in your project, please check out our article How to Install Python Packages Manually?

 

How To Install Specific Version Of Python Dependencies Locally In A Project

There are numerous distinct approaches in dealing with dependencies in Python, one of which is using pip in combination with a requirements.txt file, which allows the confining of packages and their dependencies to separate specific versions.

The pip install command automatically installs the most recent published version of a package along with its dependencies. However, in some cases, you might need to install a particular version that you know works with your code. The requirement.txt file allows you to specify precisely what packages and versions should be installed.

 

Step 1: To find out which packages are installed on your machine, run the following command.

pip freeze > requirements.txt

 


Note

This command creates a requirement.txt file that lists all the packages that are installed on your machine. The freeze command dumps all the packages with their versions to standard output in the requirements.txt file so that you can use that file to install the exact requirements into another system or environment.

When you need to replicate the environment into another system, you can run pip install indicating the requirements file using the – r switch. Additionally, you can change the versions of the packages as per your requirements and you can also delete packages that you do not need in your new environment. In the above requirement.txt file, you can change the logical operator >= to tell pip to install an exact or greater version of the published package or vice versa.


 

Step 2: Once you have finalized your requirement.txt file, simply follow the following steps from above to create a virtualenv.

 

Step 3: To install virtualenv using pip, use the following command.

pip install virtualenv

 

Step 4: Create a virtual environment in your project.

virtualenv test

 

Step 5: Activate your virtual environment.

source test/bin/activate

 

Step 6: Then install your dependencies using the pip command in combination with your updated requirement.txt file. Here we will use the -r switch.

pip install -r requirements.txt

 

In case if you return to your project after a while and it shows errors of dependencies not found. Then run source test/bin/activate again so that the dependencies can be found. Note that in this case, ‘test’ is the name of your project.

 

Alternatives Of Installing And Managing Python Dependencies

Various other open-source libraries can be useful while installing and managing Python dependencies in your projects. A few of them are:

  1. Autoenv
  2. Pipenv
  3. Pipreqs
  4. pip-check
  5. pip-name

 

Conclusion

In this article, we explored why Python dependencies are important and how one can install them locally when version control is not important and when it is important. We also explored the prescribed way of creating a virtual environment for your project when manipulating the versions of packages using the requirements file, so that other projects on your machine are not affected.

We hope this article was insightful and helped you learn new ways of installing Python dependencies locally. And as always, happy coding!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share