How to Create an Isolated Python Environment

Posted on

As stated in the title of the article, what does it take for creating an isolated python environment ?. In other words, as stated in the title of this article, how to create an isolated python environment. But first of all, the most important thing is realizing about the usage of having an isolated python environment. To keep it simple, imagine that there are two or more application exist and each of those applications are having different version of python to be used in the development process. It is obviously an advantage for having a separated environment where each environment is associated with different kind of python’s version. By creating an isolated python environment, it can have an actual selection or it can handpicked the suitable version of the python based on the needs.

The above goal or purpose described can be achieved by executing the following command :

virtualenv

To put it in an actual test, below is the scenario taken in order to create an isolated python enviroment :

user@hostname:~/test$ virtualenv myapp
New python executable in /home/user/test/myapp/bin/python
Installing setuptools, pip, wheel...done.
user@hostname:~/test$

The above command execution, it is using a command named ‘virtualenv’ with the additional parameter stated as the example shown above is ‘myapp’. That additional parameter is actually will become the name of a new directory as shown below to be listed as the command above successfully executed :

user@hostname:~/test$ ls -al
total 104
drwxrwxr-x   9 user user  4096 Jan  9 16:29 .
drwxrwxr-x 151 user user 61440 Jan  9 16:30 ..
...
drwxrwxr-x   6 user user  4096 Jan  9 16:29 myapp
...
user@hostname:~/test$ 

But what is actually exist inside the content of the folder named ‘myapp’ :

user@hostname:~/test/myapp$ ls -al
total 24
drwxrwxr-x 6 user user 4096 Jan  9 16:29 .
drwxrwxr-x 9 user user 4096 Jan  9 16:29 ..
drwxrwxr-x 2 user user 4096 Jan  9 16:30 bin
drwxrwxr-x 2 user user 4096 Jan  9 16:29 include
drwxrwxr-x 3 user user 4096 Jan  9 16:29 lib
drwxrwxr-x 2 user user 4096 Jan  9 16:29 local
user@hostname:~/test/myapp$ 

This is another output content shown below using the command tree where it is actually try to show the content of the following folder created by using a virtualenv command named ‘myapp’ :

user@hostname:~/test/myapp$ tree -L 2 . 
.
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate_this.py
│   ├── easy_install
│   ├── easy_install-2.7
│   ├── pip
│   ├── pip2
│   ├── pip2.7
│   ├── python
│   ├── python2 -> python
│   ├── python2.7 -> python
│   ├── python-config
│   └── wheel
├── include
│   └── python2.7 -> /usr/include/python2.7
├── lib
│   └── python2.7
└── local
    ├── bin -> /home/user/test/myapp/bin
    ├── include -> /home/user/test/myapp/include
    └── lib -> /home/user/test/myapp/lib
9 directories, 14 files
user@hostname:~/test/myapp$

After creating an isolated python environment, try to check the version of the python used as follows :

user@hostname:~/test/myapp$ python --version
Python 2.7.12
user@hostname:~/test/myapp$ which python
/usr/bin/python
user@hostname:~/test/myapp$

The content of the folder named ‘myapp’ can be seen in the following command executed :

user@hostname:~/test/myapp$ l
bin/ include/ lib/ local/
user@hostname:~/test/myapp$

The following is shown as the content existed in the folder named ‘bin’ where the folder itself is actually located inside of the folder named ‘myapp’ :

user@hostname:~/test/myapp$ cd bin/
user@hostname:~/test/myapp/bin$ ls
activate  activate.csh  activate.fish  activate_this.py  easy_install  easy_install-2.7  pip  pip2  pip2.7  python  python2  python2.7  python-config  wheel

This is the actual version of the python which is located in the folder created by executing the ‘virtualenv’ command as shown below :

user@hostname:~/test/myapp/bin$ ls -al | grep python
-rwxrwxr-x 1 user user 3546104 Jan  9 16:29 python
lrwxrwxrwx 1 user user       6 Jan  9 16:29 python2 -> python
lrwxrwxrwx 1 user user       6 Jan  9 16:29 python2.7 -> python
-rwxrwxr-x 1 user user    2340 Jan  9 16:30 python-config
user@hostname:~/test/myapp/bin$ ./python --version
Python 2.7.12
user@hostname:~/test/myapp/bin$ 

One thought on “How to Create an Isolated Python Environment

Leave a Reply