Welcome to the new home of PowerWire – PowerWire.uk

Update your bookmarks now!

Informing the IBM Community

python

Python Environments

5
(2)

Introduction

Whenever I conduct a workshop or training session on developing in Python, I always make it a point to cover Python environments.

The first question often asked has to be What are python environments?, and the very next is Why should I care about python environments?

This topic is crucial for anyone diving into Python, and that includes any Python development on the IBM i platform.

IBMs recommendation on using these can be found using this link.

Understanding and managing Python environments can significantly enhance your development workflow and ensure consistency across different projects.

Let us explore why Python environments are essential and how they can be effectively utilised on IBM i.

Let me start with a picture …

Here we can see three environments,

  1. Project number one. A development that has to have python running at version 2.6, yes I know it is out of date, but clients …

  2. The second environment, python is now getting a bit more up to date, it is running python 3.6

  3. And the final environment is running python at version 3.9

If I need to support and maintain these applications, it is going to be a nightmare! I will be spending more time installing and removing programs and their dependencies and then setting them all up again.

By harnessing the power of Python virtual environments, you’re stepping into a realm where your development projects can thrive in perfect isolation.

Each environment acts as its own fortress, safeguarding dependencies and preventing conflicts.

This means you can focus solely on your work.

Working this way you can dive into your work with confidence, knowing that your environment is tailored just for your project.

While installing third-party packages system-wide indeed offers the allure of efficiency, saving time and disk space, this approach conceals a lurking danger.

As your projects evolve, dependencies can clash, leading to compatibility issues that ripple through your coding. A library updated for a new feature might break another project relying on an older version.

Imagine the frustration when a late-night bug fix unearths a dependency conflict, spiralling into a frantic search for solutions or, worse, leading to downtimes.

Rather than harnessing the power of third-party packages efficiently, you find yourself trapped in a web of chaos.

And what is the remedy?

Embrace Virtual Environments

They provide the freedom to experiment and innovate without the fear of jeopardising other projects.

Each environment can house its specific dependencies, ensuring that your systems remain synchronised and stable.

In this realm, you use the best tools without the risk of unforeseen repercussions.

Navigate the tech landscape confidently, knowing that a well-structured environment is your greatest ally.

Here are some key benefits of python virtual environments:

  1. Isolation: Each virtual environment is independent, so you can have different versions of packages and dependencies for different projects without conflicts

  2. Reproducibility: You can easily recreate your environment using a requirements.txt file, which lists all the packages your project needs. This helps in sharing your environment with others or deploying it

  3. Cleaner System: By using virtual environments, you can avoid cluttering your global Python installation with numerous packages that might not be used in every project

  4. Controlled Development: You can test new libraries or versions without affecting your main development environment, minimising risks

How to Set Up a Virtual Environment

There are a number of virtual environment packages out there you can use. Personally, I always use a package called venv.

It has always been easy to use and has worked flawlessly, but other packages are also available!

Here’s a quick guide to set up a virtual environment:

Install VENV

Use PIP to install the VENV package

Bash

pip install venv
Bash

Create a Virtual Environment

Running python -m calls the module named venv and will create the environment in the sub-directory called .venv

All our developers use this naming convention so we all know what it is used for.

Bash

python -m venv --system-site-packages .venv
Bash

Activate the Virtual Environment

On Windows

Bash

.\.venv\Scripts\activate
Bash

On macOS/Linux & IBM i

Bash

source .venv/bin/activate
Bash

Install Packages

Install packages that your project requires in the virtual environment. Safely away from other projects, just what we need.

Bash

pip install package_name
Bash

Deactivate the Environment

Bash

deactivate
Bash

Delete the virtual environment

Bash
rm -rf .venv
Bash

If you need to check if you are running in a virtual environment, just run the command shown below.

Bash

which python
Bash

This will show where python is located and being executed in your virtual environment, as seen in number 4 below.

The virtual environment will also be shown on your shell prompt as can be seen below in number 5.

Number AboveDescription

1

Create the virtual environment called .venv

2

Activate the environment

3

Check where our python is running from

4

Python is running from my users .venv bin directory

5

My SSH shell prompt showing the virtual environment

Conclusion

By following these steps, you can easily create and manage isolated environments for your Python projects, allowing you to focus on development without worrying about dependency issues.

And there you have it, a complete guide to crafting and managing isolated environments for your Python projects.

Before I end this article, I will solve the riddle of why I chose that image at the top of this article. I’m sure I can see on their screen they are using virtual environments.

It would be silly not to!

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.


Comments

2 responses to “Python Environments”

  1. Hi Andy

    Great Advice thanks, I would like to see a follow up to show you how to list the installed virtual environment and how to independently update them.

    Cheers Brad

    1. Hi Brad

      Thanks for the comments. Great idea for a follow up, watch this space!

      Cheers Andy

Leave a Reply

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