venvpy:main
Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | venvpy:main [2023/05/26 10:51] (current) – [Manual import of virtualenv packages] admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | go back to [[: | ||
+ | |||
+ | ======Virtual Environments in Python ====== | ||
+ | |||
+ | =====Installation example===== | ||
+ | |||
+ | First, open a terminal and check if **pip** is installed by typing : | ||
+ | <code bash> | ||
+ | pip -V | ||
+ | </ | ||
+ | If the command does exist, it will give the version number of pip, if the **pip** command is not installed you can get it by typing : | ||
+ | <code bash> | ||
+ | sudo apt install python-pip | ||
+ | </ | ||
+ | or: | ||
+ | <code bash> | ||
+ | sudo apt install python3-pip | ||
+ | </ | ||
+ | The virtual environment is installed at user level (no sudo required) with **pip**: | ||
+ | <code bash> | ||
+ | pip install --user virtualenv | ||
+ | </ | ||
+ | If after a while **virtualenv** command is not working (No module named ' | ||
+ | <code bash> | ||
+ | pip install --user virtualenv==20.0.23 | ||
+ | </ | ||
+ | To get larger access to virtualenv commands, you can it in the **$PATH** variable by adding these 2 lines at the end of the **.bashrc** file : | ||
+ | < | ||
+ | # add virtualenv command path : | ||
+ | PATH=$HOME/ | ||
+ | </ | ||
+ | Then you can create the virtualenvs everywhere in your computer, but it may be wise to put them in a folder (e.g. " | ||
+ | <code bash> | ||
+ | cd | ||
+ | mkdir MyEnvs | ||
+ | cd MyEnvs | ||
+ | </ | ||
+ | |||
+ | Then we can create and activate our first virtualenv called **venv-opencv-27** to, for example, make some image processing with numpy and OpenCv in Python 2.7 : | ||
+ | <code bash> | ||
+ | virtualenv -p / | ||
+ | source venv-opencv-27/ | ||
+ | </ | ||
+ | If everything went well, you should see the name of the virtualenv **(venv-opencv-27)** | ||
+ | <code bash> | ||
+ | pip install numpy | ||
+ | pip install opencv-contrib-python | ||
+ | pip install Pillow | ||
+ | python -c " | ||
+ | python -c " | ||
+ | </ | ||
+ | Then your can work on your project folder. The project folder folder can be everywhere on your computer. As long as you have **(venv-opencv-27)** at the beginning of the prompt you are in the virtualenv. | ||
+ | To quit the virtualenv, just type : | ||
+ | <code bash> | ||
+ | deactivate | ||
+ | </ | ||
+ | To open back the virtual, you just need to activate it again : | ||
+ | <code bash> | ||
+ | source $HOME/ | ||
+ | </ | ||
+ | |||
+ | |||
+ | =====Manual Install with setup.py===== | ||
+ | Open the virtualenv (ex) : | ||
+ | <code bash> | ||
+ | source $HOME/ | ||
+ | </ | ||
+ | and check that the default python running is the one of the virtualenv : | ||
+ | <code bash> | ||
+ | which python | ||
+ | </ | ||
+ | should give the path of the python interpreter in the virtualenv, in our example it will be : | ||
+ | <code bash> | ||
+ | / | ||
+ | </ | ||
+ | Then you can use, the classical command for manual install of a python package : | ||
+ | <code bash> | ||
+ | python setup.py build | ||
+ | python setup.py install | ||
+ | </ | ||
+ | |||
+ | =====Manual import of virtualenv packages ===== | ||
+ | In some cases, for example when using virtualenvs in Coppeliasim python scripts, the virtualenv modules must be manually added to the import path; Example with **pyproj** | ||
+ | <code python> | ||
+ | import sys | ||
+ | # replace / | ||
+ | sys.path.append('/ | ||
+ | </ | ||
+ | |||