User Tools

Site Tools


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 [[:start|main page]]
 +
 +======Virtual Environments in Python ======
 +
 +=====Installation example=====
 +
 +First, open a terminal and check if **pip** is installed by typing :
 +<code bash>
 +pip -V
 +</code>
 +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
 +</code>
 +or:
 +<code bash>
 +sudo apt install python3-pip
 +</code>
 +The virtual environment is installed at user level (no sudo required) with **pip**:
 +<code bash>
 +pip install --user virtualenv
 +</code>
 +If after a while **virtualenv** command is not working (No module named 'virtualenv.__main__') you can try changing version; e.g. :
 +<code bash>
 +pip install --user virtualenv==20.0.23
 +</code>
 +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 :
 +<code>
 +# add virtualenv command path :
 +PATH=$HOME/.local/bin:$PATH
 +</code>
 +Then you can create the virtualenvs everywhere in your computer, but it may be wise to put them in a folder (e.g. "MyVenvs") :
 +<code bash>
 +cd
 +mkdir MyEnvs
 +cd MyEnvs
 +</code> 
 +
 +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 /usr/bin/python2.7 venv-opencv-27
 +source venv-opencv-27/bin/activate
 +</code> 
 +If everything went well, you should see the name of the virtualenv **(venv-opencv-27)**  into parenthesis at the beginning of the prompt. We can now install the packages we need in the virtualenv and check installation went fine by typing the version of **numpy** and **OpenCV** ::
 +<code bash>
 +pip install numpy
 +pip install opencv-contrib-python
 +pip install Pillow
 +python -c "import numpy as np;print np.__version__"
 +python -c "import cv2;print cv2.__version__
 +</code> 
 +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
 +</code> 
 +To open back the virtual, you just need to activate it again :
 +<code bash>
 +source $HOME/MyEnvs/venv-opencv-27/bin/activate
 +</code> 
 +
 +
 +=====Manual Install with setup.py=====
 +Open the virtualenv (ex) :
 +<code bash>
 +source $HOME/MyEnvs/venv-opencv-27/bin/activate
 +</code> 
 +and check that the default python running is the one of the virtualenv :
 +<code bash>
 +which python
 +</code>
 +should give the path of the python interpreter in the virtualenv, in our example it will be :
 +<code bash>
 +/home/newubu/MyEnvs/venv-opencv-27/bin/python
 +</code>
 +Then you can use, the classical command for manual install of a python package :
 +<code bash>
 +python setup.py build
 +python setup.py install
 +</code>
 +
 +=====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 /path/to/myenv with appropriate path to virtual environment
 +sys.path.append('/path/to/myenv/lib/pythonX.X/site-packages'
 +</code>
 +