Python Virtual Environments

Overview to conda environments

Installing miniconda

cd $HOME
mkdir $HOME/tmpconda
TMPDIR=$HOME/tmpconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
TMPDIR=$HOME/tmpconda bash Miniconda3-latest-Linux-x86_64.sh
conda config --set auto_activate_base false

Now try creating a test conda environment:

cd ~/miniconda3/envs
~/miniconda3/bin/conda create -n testenv2_3.11.4 -c conda-forge time numpy requests pygrib netCDF4 scipy python=3.11.4
~/miniconda3/bin/conda init bash
conda activate $HOME/miniconda3/envs/testenv
cd $HOME/miniconda3/envs/testenv
conda deactivate

Other useful commmands (see https://www.python.org/downloads/ for specific python versions) :

conda create -n testenv2 python=3.11.3
conda activate testenv2

conda update conda (update just the conda package)
conda update --all (update all conda packages)
python3 -m pip list --outdated
python3 -m pip install --upgrade <package_name>

conda install conda=23.3.1 (installing or force-installing a specific conda package version)

conda -V (the conda version)

conda remove --name myenv --all (remove a conda environment)

Running a cron under conda (is this correct?)

21 15 * * * conda activate testenv2; /path/to/script.py > /tmp/output.txt (just call the correct version of python in first line of the script)

Questions/Tests:

Using venv to set up a python virtual environment

Note: This method does not allow for running a different version of python that is natively installed on the system.

python3 -m venv venv_test
source venv_test/bin/activate
cd venv_test
pip list
pip install --upgrade pip
pip install pygrib

(venv_test) (base) [user@host: ~/venv_test]$ pip list
Package    Version
---------- ---------
certifi    2022.6.15
numpy      1.21.6
pip        22.1.2
pygrib     2.1.4
pyproj     3.2.1
setuptools 40.6.2

When done working:

deactivate

Setting up a python virtual environment (with virtualenv)

Note: This method does allow for running a different version of python that is natively installed on the system.

dnf install virtualenv
virtualenv --python="/usr/local/bin/python" "/path/to/the/venv_dir/"
cd /path/to/the/venv_dir
source bin/activate

See Python Alternative Upgrade Notes for installing a different/local version of python.