Project

General

Profile

Python and virtualenv » History » Version 4

Alexis Jeandet, 20/05/2016 08:19 PM

1 3 Alexis Jeandet
# **Not complete!** 
2 1 Alexis Jeandet
# Python and virtualenv
3
4
## Why?
5
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.
6
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.
7
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.
8
Like this you will be able to have as many virtual environments as you want with different packages and different versions in each environment.
9
10
## How?
11
12
1. First get virtualenv
13
14
 On Fedora:
15
16
~~~bash
17
sudo dnf install python*-virtualenv
18
~~~
19
Will install both Python2 and Python3 versions of virtualenv.
20
21
2. Create your environments
22
23
~~~bash
24
mkdir /opt/Py2Venv /opt/Py3Venv
25 4 Alexis Jeandet
virtualenv-2.7 /opt/Py2Venv
26
virtualenv-3.4 /opt/Py3Venv
27 1 Alexis Jeandet
~~~
28
29 4 Alexis Jeandet
Now you have two basic virtual environments for Python 2 and 3. Note that you can add the --no-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 without --no-site-packages flag.
30 1 Alexis Jeandet
31
3. Use it 
32
33
Your environment is ready to play, to use it you have to **activate** it.
34
35
~~~bash
36
source /opt/Py3Venv/bin/activate
37
~~~
38
39
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.
40
If the activate command worked you may see you shell prompt like this:
41
42
~~~
43
(Py3Venv)[adminlpp@pc-instru opt]$ 
44
~~~
45
Note the **(Py3Venv)** this says that Py3Venv is activated. To quit/deactivate it just use the command **deactivate**.
46
47
Let's install Pandas, Jupyter, numpy...
48
49
~~~bash
50 2 Alexis Jeandet
(Py3Venv)[adminlpp@pc-instru opt]$ pip install pandas
51
(Py3Venv)[adminlpp@pc-instru opt]$ pip install jupyter
52 1 Alexis Jeandet
~~~