Project

General

Profile

Python and virtualenv » History » Revision 9

Revision 8 (Alexis Jeandet, 26/05/2016 04:44 PM) → Revision 9/12 (Alexis Jeandet, 26/05/2016 04:45 PM)

# **Not complete!**  
 # Python and virtualenv 

 ## Why? 
 Usually with Python you may ear from your colleagues "Hey, you may use package X to do this it's much better than Y and easier" then you discover that package X isn't packaged in your distribution or you install it and it mess up your system. For example Jupyter isn't packaged yet in Fedora and installing it directly with pip may break your already installed version of IPython-3x. 
 To use last version of your favourite Python tools such as Jupyter notebooks or Pandas on your system without messing up your computer, virtualenv is a solution. 
 It will allow you to install any python package with pip in an isolated environment so your system will not see your packages until you activate it. 
 Like this you will be able to have as many virtual environments as you want with different packages and different versions in each environment. 

 ## How? 

 1. First get virtualenv 

  On Fedora: 

 ~~~bash 
 sudo dnf install python*-virtualenv 
 ~~~ 

 On Mac OS with port: 

 ~~~bash 
 sudo port install py35-virtualenv 
 sudo port install py27-virtualenv 
 ~~~ 

 Will install both Python2 and Python3 versions of virtualenv. 

 2. Create your environments 

 ~~~bash 
 sudo mkdir /opt/Py2Venv /opt/Py3Venv 
 sudo chown -R <yourLogin> /opt/Py2Venv /opt/Py3Venv 
 virtualenv-2.7 --system-site-packages /opt/Py2Venv 
 virtualenv-3.4 --system-site-packages /opt/Py3Venv 
 ~~~ 

 Now you have two basic virtual environments for Python 2 and 3. Note that you can remove the --system-site-packages flag to tell that you want your environment to ignore system-wide packages it may protect you from some local and global packages incompatibilities. In most cases you may create it with --system-site-packages without --no-site-packages flag. 

 3. Use it  

 Your environment is ready to play, to use it you have to **activate** it. 

 ~~~bash 
 source /opt/Py3Venv/bin/activate 
 ~~~ 

 Then it will override your system pip command by your current virtualenv one and all packages installed with pip while your environment is activated will be installed in your environment. 
 If the activate command worked you may see you shell prompt like this: 

 ~~~ 
 (Py3Venv)[adminlpp@pc-instru opt]$  
 ~~~ 
 Note the **(Py3Venv)** this says that Py3Venv is activated. To quit/deactivate it just use the command **deactivate**. 

 Let's install Pandas, Jupyter, numpy... 

 ~~~bash 
 (Py3Venv)[adminlpp@pc-instru opt]$ pip install pandas 
 (Py3Venv)[adminlpp@pc-instru opt]$ pip install jupyter 
 ~~~