Creating virtual environments for Python for z/OS
Sujit Neb - BMC Software

Creating virtual environments for Python for z/OS

Creating virtual environments in Python is a best practice for several reasons:

  1. Isolation of Dependencies: Virtual environments allow you to create isolated environments for different projects. Each environment can have its own set of dependencies, preventing conflicts between packages required by different projects. This is particularly useful when different projects may require different versions of the same package.
  2. Version Compatibility: Virtual environments enable you to specify and control the version of Python and third-party libraries for a particular project. This ensures that your project is compatible with the specific versions of packages it depends on, reducing the risk of unexpected behavior due to version mismatches.
  3. Easy Reproducibility: By using a virtual environment, you can create a requirements.txt file that lists all the dependencies along with their versions. This makes it easy for others (or yourself in the future) to recreate the exact environment needed for the project. Reproducibility is crucial for collaboration and deployment.
  4. Sandbox for Testing: Virtual environments provide a sandbox for testing and experimenting with different packages or versions without affecting the global Python environment. This is especially important when developing or testing software to ensure that changes do not have unintended consequences.
  5. Cleaner Development and Deployment: When you're developing a Python application, having a virtual environment helps keep your development environment clean. It avoids cluttering the global Python environment with project-specific dependencies. It also simplifies deployment, as you can package and deploy only the necessary dependencies for your project.
  6. Cross-Platform Compatibility: Virtual environments make it easier to work on different platforms. If your project relies on specific packages or versions, the virtual environment ensures a consistent environment across different operating systems.

Overall, creating virtual environments is a good practice for managing dependencies, improving code quality, and facilitating collaboration in Python development.

IBM's Open Enterprise SDK for Python provides the venv module for creating lightweight virtual environments. This module allows you to manage separate package installations for different projects.

So, to create a virtual environment, run the venv module as a script with the directory path as the following command:

python3 -m venv  /path/to/new/virtual/environment        

The previous command creates a target directory and a bin subdirectory that contains a copy of the Python binaries files and link to standard libraries. If you want to pull all packages bundled with IBM Open Enterprise SDK for Python into the virtual environments, run the above command with --system-site-packages option:

python3 -m venv  /path/to/new/virtual/environment --system-site-packages        

The previous command creates the virtual environments that contain all the IBM Open Enterprise SDK for Python bundled packages such as: cffi, cryptography, and so on.

IBM Open Enterprise SDK for Python contains rebuilt PyPI packages. By default, creating a virtual environment creates a clean environment, which means no packages installed. Specifying the --system-site-packages flag exposes these additional packages contained within IBM Open Enterprise SDK for Python, so that they can be used within your virtual environment. This action is required if you need to install a package that has dependencies on one of these bundled packages. Otherwise, pip installs packages from PyPI which can lead to installation failure.

Once you create a virtual environment, you can activate it by running the following line:

. </path/to/new/virtual/environment/>/bin/activate        

For more information about installing packaging by using pip and virtual environments, see Install packages in a virtual environment using pip and venv

This article is written help new users to quickly get into running their first python program on z/OS. Also, I have written one more article that may help you boost your speed to work with python, refer: Get your Python AI Toolkit for IBM z/OS

Feel free to share this to your friends, colleagues if they are new to python for z/OS or if they find it useful to kickstart their journey with IBM Z.

To view or add a comment, sign in

More articles by Sujit Neb

Others also viewed

Explore content categories