How to Install Python Packages Using a Script

How to Convert PNG to TIFF in Python?

If you are a twenty-first-century developer who develops vast applications, isn’t it a tiresome journey to install Python packages every time you develop a new application?

It is a nightmare to enter commands in the command-line interface to install the same packages again and again for developing each new application.

What if there was a way to automate this process?

Do not worry, as this can be achieved via scripting, which allows us to use just one command in the command prompt that will have all the required packages automatically installed.

In this brief article, we shall discuss the necessity of Python packages and the automating technique, ‘scripting’, along with different requirements for installing packages via script. Furthermore, we shall also discuss how to provide interactivity for enhancing the script.

 

What Is A Python Package?

When working on a very large application with many modules, it becomes difficult to keep track of this many modules.

It happens because modules have similar names or they even have the same functionality. At this point, one thinks of a way to organize and manage these files. The solution is the hierarchical structuring in the form of packages, which allows us to avoid any discrepancy which may arise due to module name or functionality.

So, a Python package is a set of modules. These modules are somewhat related to each other and therefore are placed on the same package.

Since there are numerous Python packages already available on open-source platforms. This availability of ready-to-install Python packages has tremendously improved the time efficiency and reliability of Python-based development.

 

The Python Installation Check Before Installing A Package

  • Verify that Python is installed:

Open a Windows command window and run the following command to check that a suitable Python version is installed:

python --version

The output should be similar to:

Python 3.6.6.

  • Verify that Pip is installed:
pip --version

The output should be similar to:

pip 19.3.1

  • Update Pip and Wheel to ensure you have the latest version installed:
pip install --upgrade pip wheel

 

Why Automate The Process Of Package Installation

Tired and annoyed about installing Python packages for each new script or system that you work on? While also thinking of making your life easier instead of typing repetitive Python codes again?

Then why not automate this using simple scripts.

Since packages allow us to use codes that can be repeated to reduce our overall coding time, rather than typing our code each time we want to use it. Therefore, packages are most importantly useful in utilizing the available online pre-coded resources instead of writing everything from scratch each time. We must automate this process and remove the burden of manually installing packages.

Let’s look at the possibilities of its implementation (later in this article).

 

Limitations In Using Pip In Python Script To Install Packages

One of the basic limitations of using Pip in Python script to install packages is that it is not supported by the Python Packaging Authority (PyPA) because pip is not thread-safe, and Pip always runs as a single process.

When you use pip in the thread form, the results might not be as you expected them to be. Pip might not install the package successfully and unexpected output might be observed since it may also affect code apart from pip.

 

The Solution To Above Limitations: Using Pip In A Subprocess

Since pip assumes that once it has finished its work, the process will terminate. There does not exist a possibility for pip that the code will run further from its point of termination, and therefore, issues are likely to arise if it is run with other processes.

Among many other options available, the most reliable approach, and the one that is fully supported to automate the task at hand, is to run pip in a subprocess.

PyPA fully allows and supports using a Python script to run pip as a subprocess. This is an easy approach and is explained in the following section.

Just a few lines of code can automate the installation of Python packages. This is done by creating a Python script that runs pip as a subprocess:

import sys

import subprocess

# implement pip as a subprocess:

subprocess.check_call([sys.executable, '-m', 'pip', 'install','<packagename>'])

Note: When this script is initiated, all the packages with their requirements will be installed.

 

How To Make The Script Interactive

Use one of the other APIs in the module to process the output further. This makes the script interactive. If you are running the script interactively, it’s good practice to confirm the installation.

The following script will first run pip as a subprocess to install one or more packages, and later it will print an updated list of installed packages. For this purpose, we are going to use freeze here, which outputs installed packages in requirements format.

import sys

import subprocess

 

# implement pip as a subprocess:

subprocess.check_call([sys.executable, '-m', 'pip', 'install',

'<packagename>'])

 

# process output with an API in the subprocess module:

reqs = subprocess.check_output([sys.executable, '-m', 'pip',

'freeze'])

installed_packages = [r.decode().split('==')[0] for r in reqs.split()]

 

print(installed_packages)

 

How To Use A Script While Using Conda As Subprocess

For users working on Anaconda’s version of Python, one can run Anaconda’s package manager (Conda) as a subprocess to install Python packages. The script for this purpose is as follows:

import sys
import subprocess

import conda.cli.python_api as Conda

 

# implement conda as a subprocess:

 

subprocess.check_call([sys.executable, '-m', 'conda', 'install',

'<packagename>'])

Note: The specified package(s), along with any requirements, will be installed and displayed as output.

 

Conclusion

In this brief article, we explored the importance of packages, the general requirements as well as how to install Python packages using a script, and the availability of thousands of open-source ready-to-use packages.

We further discussed the limitations of automating package installation and its solution. We learned why pip has to be called out as a subprocess instead of being used in a thread.

We also looked upon the interactive feature of the script using the freeze command to output the updated installed packages list.

Lastly, the implementation of Conda as a subprocess for the users working on Anaconda’s version of Python. We hope this information helped you 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