Christopher Juckins

SysAdmin Tips, Tricks and other Software Tools

User Tools

Site Tools


python_virtual_environments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
python_virtual_environments [2023/04/28 11:56] juckinspython_virtual_environments [2023/08/10 11:34] juckins
Line 1: Line 1:
 ==== Python Virtual Environments ==== ==== Python Virtual Environments ====
  
-[[https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533#7d04|Overview to conda environments]]\\ +[[https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533#7d04|Overview to conda environments]]
-Crons under conda [[https://stackoverflow.com/questions/36365801/run-a-crontab-job-using-an-anaconda-env|solution 1]] and [[https://askubuntu.com/questions/1341422/how-to-run-my-python-script-with-anaconda-environment-in-crontab|solution2]]+
  
 __Installing miniconda__ __Installing miniconda__
Line 17: Line 16:
  
   * (Accept license agreement and all the suggested default choices)   * (Accept license agreement and all the suggested default choices)
 +  * Note that if you do not want conda's base environment to be activated upon startup, run the following:
  
-  ~/miniconda3/bin/conda create -n testenv -c conda-forge time numpy requests pygrib netCDF4 scipy+  conda config --set auto_activate_base false 
 + 
 +Now try creating a test conda environment: 
 + 
 +  cd ~/miniconda3/envs 
 +  ~/miniconda3/bin/conda create -n testenv2 python=3.11.4 -c conda-forge time numpy requests pygrib netCDF4 scipy
   ~/miniconda3/bin/conda init bash   ~/miniconda3/bin/conda init bash
  
Line 30: Line 35:
   conda deactivate   conda deactivate
      
-Other useful commmands: +Other useful commmands (see https://www.python.org/downloads/ for specific python versions) 
-  conda create -n testenv2 python=3.11.3 (with specific version of python)+  conda create -n testenv2 python=3.11.3
   conda activate testenv2   conda activate testenv2
      
Line 44: Line 49:
      
   conda remove --name myenv --all (remove a conda environment)   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: Questions/Tests:
   * Create a conda test env with specific version of python   * Create a conda test env with specific version of python
   * Run a script using that new test environment   * Run a script using that new test environment
 +    * Update the first line to point to the correct version of python
 +  * Run cron under conda
 +    * If you update the first line in the script to point to desired conda version of python, then no need to activate the conda env (?)
  
-__Setting up a python virtual environment__+__Using venv to set up a python virtual environment__
  
 <color #ed1c24>Note: This method __does not__ allow for running a different version of python that is natively installed on the system.</color> <color #ed1c24>Note: This method __does not__ allow for running a different version of python that is natively installed on the system.</color>
python_virtual_environments.txt · Last modified: 2023/08/10 11:35 by juckins