How to Install Python Module Within Code

Oluwafisayo Oluwatayo Feb 02, 2024
  1. Benefits of Installing Python Module With Codes
  2. Install Python Code With Codes
  3. Install Multiple Python Modules Within Code
  4. Install Python Modules with Specific Version Within Code
  5. Conclusion
How to Install Python Module Within Code

Ideally, it is very convenient to install Python modules from pip; to do that, you have to type in pip install module-name inside an active terminal, and you’re done.

But today, we want to learn how to install modules using Python scripts.

Benefits of Installing Python Module With Codes

Installing modules with Python scripts poses two awesome benefits compared to installing Python modules within the terminal.

First, it allows us to install more than one module simultaneously; we will work on an example that shows this.

Secondly, we can install a specific version of the module we want. This is ideal if the version we want to install has a specific feature that other versions lack.

Install Python Code With Codes

In this example, we will install just one Python module within the code. Create a new Python file; you can name it new.py and input this little snippet of code:

import os

os.system("pip install bottle-json-pretty")

We insert the install command inside os.system, click run, and we should see the successful installation message in the terminal.

Install Multiple Python Modules Within Code

We want to install multiple Python modules within the code in this example. This feature is soothing in situations where we want to newly set up a new Python environment and want to install the required packages to kick-start usage of the IDE.

Create a new Python file, name it new.py and insert these codes:

import sys
import subprocess
import pkg_resources

required = {"sysdweb", "Flask-OIDC-SP", "apm-client", "Glances", "BottleJwtAuth"}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed

if missing:
    subprocess.check_call([sys.executable, "-m", "pip", "install", *missing])

Inside REQUIRED = {}, that’s where we added the modules we wish to install.

Install Python Modules with Specific Version Within Code

This example allows us not only to install a specific version of the module but can also install multiple versions; this is a complete package.

Create a new file, name it new.py and type in these codes:

import sys
from subprocess import run, PIPE, STDOUT
import pkg_resources


def run_cmd(cmd):
    ps = run(cmd, stdout=PIPE, stderr=STDOUT, shell=True, text=True)
    print(ps.stdout)


required = {"markdown-server", "semver==2.9.0"}
installed = {f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set}
missing = required - installed

if missing:
    run_cmd(f'pip install --ignore-installed {" ".join([*missing])}')

Conclusion

This method helps because you can create a package of your favorite Python modules and put them within the code. By doing such, you have a Python script or file that you can save and take everywhere you want.

You can easily use another IDE you want to set up without the need to keep perusing the pip command to install modules.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn

Related Article - Python Installation