Creating virtual environments for Python for z/OS
Creating virtual environments in Python is a best practice for several reasons:
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:
Recommended by LinkedIn
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.
Love this
Wow