python_virtual_environments
This is an old revision of the document!
Python Virtual Environments
Overview to conda environments
Installing miniconda
- Download and install miniconda3 as non-admin user from https://docs.conda.io/en/latest/miniconda.html
- Get the "Miniconda3 Linux 64-bit" version
- NOTE: You may need to remove a $HOME/.condarc file if you have one to prevent repo source conflicts
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
- (Accept license agreement and all the suggested default choices)
~/miniconda3/bin/conda create -n testenv -c conda-forge time numpy requests pygrib netCDF4 scipy ~/miniconda3/bin/conda init bash
- (Open a new xterm and type 'bash')
conda activate $HOME/miniconda3/envs/testenv cd $HOME/miniconda3/envs/testenv
- (do work)
conda deactivate
Other useful commmands:
conda create -n testenv2 python=3.11.3 (with specific version of python) 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
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:
- Create a conda test env with specific version of python
- Run a script using that new test environment
- Update the first line to point to the correct version of python
- Run cron under conda
- As long as you update the first line in the script to point to the desired version of python, then you don't need to activate the conda env (?)
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.
python_virtual_environments.1682711007.txt.gz · Last modified: 2023/04/28 15:43 by juckins