Creating Isolated Python Environments with Virtualenv

Why bother? Because with virtualenv, we can create multiple Python environments on one computer that each:

  1. Are capable of running different versions of Python. Right now I have both Python 2.7 and Python 3.8 installed and am able to create either environment and run code with that version’s Python interpreter.
  2. Isolate dependencies for external libraries. Once the environment is created, I do not have to worry about different versions of Python libraries conflicting with each other.
  3. Allow you to have a unique Python environment for different projects.

Potential Pitfalls to Avoid with Virtual Environments

  1. Know your OS. The virtualenv commands are slightly different from the Mac, or Linux OS to Windows.
  2. Be aware of variations within Windows systems. Some Stack Overflow post commands mention a “Bin” folder, however on my particular version of Windows, the folder was named “Scripts” instead. There was no “Bin” folder that I could locate.
  3. Choose the right library. Virtualenv is used commonly. Virtualenvwrapper is a handy tool to make it easier to use. I chose virtualenv as my virtual environment library because I wanted to maintain both Python 2.7 and 3.8 environments. For versions of Python 3.3 and above, check out venv, an easy to use, stock library and virtual environment generator. I am looking forward to checking both of these out. Virtualenv can be installed with the pip installer.

Consider upgrading your Python version to the latest stable release. Follow these steps to download and install Python 3.8 in the Ubuntu terminal.

Install pip in your new Python version:

curl -O https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py

Install Virtalenv on Ubuntu: sudo pip3 install virtualenv

Create a virtual environment with your new Python version. Enter in terminal or command prompt: virtualenv -p python3.8 38env

Activate your new virtual environment on Ubuntu:

cd Desktop/Projects/Sandbox38/bin && source activate

Alternatively, activate an env on Windows:

cd Desktop\Projects\your_env_name\scripts & activate

Activation and Deactivation for Mac vs. Windows

Now you can now install pandas 1.0 in your virtual environment.

python3.8 -m pip install pandas

Below are two screenshots of the command prompt and some links that helped me.

Below, I am creating a 2.7 environment, even though I have Python 3.6 as my system version. Note how many times I messed up before I got it right, thanks to a Stack Overflow post. It took many previous tries but I eventually figured out how to create a Python 2.7 virtualenv, which I named py27_env. I ran the python.exe file inside of the folder where I installed Python 2.7, which I named py27.

To run a program, I entered python some_program.py. I also tested by downloading modules that did not have a version compatible with Python 3.6. It worked and installed the correct Python 2 version in my py27_env environment.

create_py2_env

Below, I am activating a previously created environment named 14pandas. Then I am installing two external Excel libraries, pandas and xlrd in my environment. The one-liner posted above is a more efficient way to activate a virtualenv.

virtualenv_Activate

More Reading on venv, pipenv, and virtualenv

5 thoughts on “Creating Isolated Python Environments with Virtualenv

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.