The scaring "virtualenv"

in python •  7 years ago  (edited)

Since I’ve tried to use or learn how to use Python, I’ve always heard this scaring word virtualenv (1). My colleagues spoke about it as the key to succeed in using Python.

In fact I’ve almost destroyed my ubuntu installation in trying to install python modules without succeeding. The most dangerous parts were: modules cross dependencies, following API changes. Something that I was not used to because I had worked up to then only with languages such as C or MATLAB where everything is easier or in such a way under my direct control.

So one day I decided to fight against the dark side and dive into the “virtualenv world”. I was really surprised. It was so much easier than I supposed. It is just to create isolated python environments.

On each of them you can easy use your magic “pip” (2) tool to install all the modules you like.

I must be immediately clear that in any case virtualenv is not a tool to install python. Python installation is another scaring thing that I would like to speak about in the next posts (Why install something? Maybe would it better a Python container?)”. So before going on you should check to have python and pip already installed. To check these two points, please execute from your prompt the following commands:

python --version
pip --version

If you have no error, you are safe and you can go on. The next boring step is now to check if you have “virtualenv” installed; again try this command:

virtualenv --version

If you have error, it will be easy to install it thanks to pip tool:

pip install virtualenv

Ok, I promise you, this is the last thing to check. Now we can really start in using virtualenv.

The main idea is to organize different developing projects into independent and isolated directories. Every project must have its own directory. So suppose you are going to start the new project “be_a_millionaire”, you should execute from a proper directory (maybe something like ~/python/):

virtualenv be_a_milionare

If everything is fine you will see the following messages:

New python executable in be_a_milionare/bin/python
Installing setuptools, pip...done.

The initial virtualenv setup is completed in a few seconds. To understand what it happen, please check the content of your working directory. You will find a subdirectory called just like the name of the project. The content of this new subdirectory is:

drwxrwxr-x 6 mysuername mygroup 4.0K Feb 27 18:04 .
drwxr-xr-x 88 myusername mygroup 20K Feb 27 18:04 ..
drwxrwxr-x 2 myusername mygroup 4.0K Feb 27 18:04 bin
drwxrwxr-x 2 myusername mygroup 4.0K Feb 27 18:04 include
drwxrwxr-x 3 myusername mygroup 4.0K Feb 27 18:04 lib
drwxrwxr-x 2 myusername mygroup 4.0K Feb 27 18:04 local

The “bin” subdirectory contains a copy of the python executable and some useful scripts to manage the virtualenv itself. The other directory are used to contained instead the modules we are going to install.

Now you have your first environment ready, but be careful because the system cannot know which virtualenv or default python installation you would like suing. You must be activated environment before using them. So first of all to know which environment you are going to use please use the following command:

which python

The output of this command is the full path of the python you are going to use.
If the result is something like “/usr/bin/python”, it means you are using the default python installation, if instead it is something like “python/be_a_milionare/bin/python” it means you are using the virtualenv just created.
So the next point is how to activate the proper virtualenv. The virtualenv itself has installed all what you need to select the desired virtualenv.

For example to activate our “be_a_milionare” virtualenv, you should execute command:

source python/be_a_milionare/bin/activate

This script is automatically created with any virtualenv. Now you can start install in the activated virtualenv all the python modules you need simply using tool pip.

Useful links, credits and reference.
1 https://pypi.python.org/pypi/virtualenv
2 https://pypi.python.org/pypi/pip

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @marcobasic! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @marcobasic! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!