User Tools

Site Tools


deptlab:python_virtual_env

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
deptlab:python_virtual_env [2021/10/21 21:14] localadmindeptlab:python_virtual_env [2024/09/17 17:18] (current) – [Using Virtual Environments] localadmin
Line 24: Line 24:
  
 There are no limits to the number of environments you can have since they’re just directories containing a few scripts. Plus, they’re easily created using the virtualenv or pyenv command line tools. There are no limits to the number of environments you can have since they’re just directories containing a few scripts. Plus, they’re easily created using the virtualenv or pyenv command line tools.
 +
 +===== Using Virtual Environments =====
 +
 +CS Lab and shared research computers should already have the ''venv'' module from the standard library installed.
 +
 +Start by making a new directory to work with:
 +
 +$ mkdir python-virtual-environments && cd python-virtual-environments
 +
 +Create a new virtual environment inside the directory:
 +
 +''python3 -m venv project_1_env''
 +
 +**Note:** By default, this will not include any of your existing site packages.
 +
 +In the above example, this command creates a directory called project_1_env, which contains a directory structure similar to this:
 +<code>
 +├── bin
 +│   ├── activate
 +│   ├── activate.csh
 +│   ├── activate.fish
 +│   ├── easy_install
 +│   ├── easy_install-3.8
 +│   ├── pip
 +│   ├── pip3
 +│   ├── pip3.8
 +│   ├── python -> python3.
 +│   ├── python3 -> /usr/bin/python3
 +├── include
 +├── lib
 +│   └── python3.8
 +│       └── site-packages
 +├── pyvenv.cfg
 +└── share
 +</code>
 +
 +Here’s what each folder contains:
 +
 +  * bin: files that interact with the virtual environment
 +  * include: C headers that compile the Python packages
 +  * lib: a copy of the Python version along with a site-packages folder where each dependency is installed
 +
 +In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
 +
 +  $ source project_1_env/bin/activate
 +  (project_1_env) $
 +  
 +Notice how your prompt is now prefixed with the name of your environment (''project_1_env'', in this case). This is the indicator that ''project_1_env'' is currently active, which means <color #ed1c24>the python executable will only use this environment’s packages and settings.</color>
 +
 +Now you can use pip to install packages (large packages may take a while to install).  
 +
 +To go back to the “system” context, execute a deactivate:
 +
 +  (env) $ deactivate
 +  $
 +
 +Now your shell session is back to normal, and the python command refers to the global Python install. Remember to do this whenever you’re done using a specific virtual environment.
 +
 +Using Virtual Environment makes it east to reset your Python environment if you need to use a different set of packages.  Just delete the old environment and create a new one.
 +
 +**Be sure to cleanup/delete unused temp files and environments when done using them.**
 +
 +===== Tips for Using a Python3 IDE with Virtual Environments =====
 +
 +==== Using Spyder in the 244 Lab ====
 +  - Activate (start) your virtual environment.
 +  - Run spyder 
 +  - Go to Tools --> preferences --> python Interpreter and select the python file from the virtual env you want to link to Spyder ex : /home/you/envs/your_env/bin/python
 +
 +==== Preferred Method on Research Computers: Spyder Method 1 (Use with tensorflow) ====
 +To use Spyder.  
 +  - Activate (start) your virtual environment.
 +  - Install the needed packages with pip
 +  - Install Spyder into your environment using pip install spyder.
 +  - Then start spyder from the environment command line with the command spyder.
 +
 +==== Spyder Method 2 (Doesn't work well with tensorflow) ====
 +
 +  - Activate (start) your virtual environment.
 +  - Run spyder from the environment (after source activate)
 +  - Go to Tools --> preferences --> python Interpreter and select the python file from the env you want to link to spyder ex : /home/you/envs/your_env/bin/python
 +
 +
 + [[https://realpython.com/python-virtual-environments-a-primer/|See... Using Python Virtual Environments]] for more detail
deptlab/python_virtual_env.1634850840.txt.gz · Last modified: by localadmin