====== Differences ====== This shows you the differences between two versions of the page.
|
python:env [2016/05/16 04:48] admin created |
python:env [2016/05/16 05:05] (current) admin [Virtual Environments] |
||
|---|---|---|---|
| Line 15: | Line 15: | ||
| We first need to globaly install virtualenv from pip: | We first need to globaly install virtualenv from pip: | ||
| <code> | <code> | ||
| - | > pip install virtualenv | + | > sudo pip install virtualenv |
| </code> | </code> | ||
| + | |||
| + | Then create a project folder and create a corresponding virtual environment: | ||
| + | <code> | ||
| + | > cd ~/path/to/my-project/ | ||
| + | > virtualenv venv | ||
| + | [> virtualenv -p /usr/bin/python3 venv] # to use a specific Python interpreter | ||
| + | </code> | ||
| + | |||
| + | Now that our virtual environment is created, we need to activate it: | ||
| + | <code> | ||
| + | [> cd /path/to/my-project] | ||
| + | > source venv/bin/activate | ||
| + | </code> | ||
| + | |||
| + | The name of the current virtual environment will now appear on the left of the prompt: | ||
| + | <code> | ||
| + | > (venv) pi@raspberrypi: ... | ||
| + | </code> | ||
| + | |||
| + | From that point, any package that we install using pip will be placed in the venv folder, isolated from the global Python installation. Like this: | ||
| + | <code> | ||
| + | > pip install requests | ||
| + | </code> | ||
| + | |||
| + | Once we are done working in the virtual environment, we deactivate it: | ||
| + | <code> | ||
| + | > deactivate | ||
| + | </code> | ||
| + | |||
