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