Welcome to the Info TEST server!

Skip to content. | Skip to navigation

Sections
Info Services > Computing Guide > Software and Operating Systems > Python: isolation and management

Python: isolation and management

How to install and maintain your own python modules and/or versions of python. Also how to isolate your python code from OS updates.

There are many ways to run and maintain your own python and/or python modules.  Here are some tools and examples to do so.

Python

This level just uses the default python, or optionally installed python, that comes with the OS.  You simply write a script and run it.

Examples

#!/usr/bin/python

or

#!/usr/bin/env python3.12

 

pip

Here, you need a python module that doesn't come with the version(s) of python installed with the OS.  This will install the python module in your home account (E.g. ~/.local/lib/python3.9/site-packages).

 

pip install --user casatools



Python Virtual Environment (venv)

Creating a Python Virtual Environment allows other users to run your code without having to install their own copy of the needed python modules.  The venv is just a directory.  Below is an example that creates a venv called my-env

 

This will create, in your current directory, a directory named "example" that contains the virtual environment.

python -m venv example

 

To use a virtual environment, you must activate it by sourcing the activate script inside the environment.

source example/bin/activate

 

Once you have activated the virtual environment you can install python packages into it as if you were the administrator of the machine. This differs from using pip to install python packages into your account (see above) in that the new package only exists in the virtual environment, and not in all instances of python that you invoke outside of the virtual environment.

While the virtual environment is active your prompt will change to indicate you are inside a virtual environment.

(example)$ 

 

You may want to upgrade pip before you use it to install modules

pip install --upgrade pip

 

Then you can install modules.  Here is an example installing numpy

pip install numpy


When you are finished with your virtual environment, you can deactivate it with the deactivate command.

deactivate

 

To delete a virtual environment, just delete its directory.  Name your virtual environments wisely, so that you can remember what they are days, weeks, or even months later.

 

 

miniforge

This is useful if you need a version of python that is not provided by the OS, or you just want to isolate yourself from the version provided by the OS.  It allows you to keep using the same version of python regardless of what version is on the OS.  So when your IT staff updates the OS, and a different version of python gets installed, your code should still work.

Using miniforge is very similar to using "conda" but without any licensing concerns.


Download and install

https://conda-forge.org/download/
bash Miniforge3-$(uname)-$(uname -m).sh

 
or for the CLI enthusiasts

wget -O Miniforge3.sh "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3.sh -b -p "${HOME}/conda"

  
Run the environment

source "${HOME}/conda/etc/profile.d/conda.sh"


It will install a default version of python.  In 2025 this seems to be python-3.12.  You can install a different version (e.g. python-3.10) like so


Install a different version of python with something like

conda create --name py3.10 python=3.10


Activate the py3.10 environment

conda activate py3.10

 
To deactivate the two environments

conda deactivate
conda deactivate



container

A container is useful when you need to make your code portable so that others not at the NRAO can use it.

 

Search All NRAO