Custom Search

Wednesday, December 15, 2010

Howto Python Django Virtualenv setup

Howto Python Django Virtualenv setup

Virtualenv can download it for free at http://pypi.python.org/pypi/virtualenv.

What virtualenv does is create a new isolated or “virtual” Python environment.
It accom-plishes this by creating a new directory that contains these items:

* A copy of the Python interpreter
* A directory for executable Python scripts
* A site-packages directory for Python modules
* A tool called easy_install that you can use to download and install new Python modules
* Scripts that you can use to “activate” the virtual environment

This Python interpreter will be set up to use the site-packages directory created by
virtualenv rather than the system-wide site-packages directory normally used by Python.
When the virtual environment is active, any Python packages you install (whether via
easy_install, some other tool, or a manual setup.py install) will install into the virtual
environment’s site-packages directory.

* Any libraries you install will be “visible” only to that environment.

* Because different virtual environments don’t interfere with one another,
you can have two projects that use two different modules of the same name—each project
will see only the module that’s installed in its environment.

* If you have two projects that need different versions of a library,
you can simply install the appropriate version in each project’s virtual environment.

1)
Downloaded and install virtualenv.

Downloaded virtualenv choose the source package and use the setup.py script it provides to
install.

OR

#apt-get install python-setuptools <--- This will install the tool 'easy_install'.
#easy_install virtualenv
<------- OR #apt-get install python-virtualenv <------- 2)
To create a new virtual Python environment, open a command line and type:
#python /path/to/virtualenv.py new_environment_name <-------
OR # virtualenv new_environment_name

This will create a new directory called 'new_environment_name',
containing the new Python environment. Inside it is a directory named bin, whose contents
depend on your computer’s operating system:

In LINUX there will be a single script named activate,which is written in thestandard UNIX
bash scripting language.

To run it, simply type "source activate" from a command line in the environment’s bin directory.

To deacti-vate the environment, just close the terminal window or type the command "deactivate".

3)
Once you’ve activated your virtual environment, typing 'python' (
in the same command-line session) will run the virtual environment’s Python interpreter.
The interpreter will then look for Python modules in the virtual environment’s site-packages
directory.

From there you can, for example, install Django by typing:
#easy_install Django <------- This will download the latest Django release package and
install it in the virtual environ-ment’s site-packages directory. You can also place any other
Python modules you’d like in the virtual environment’s site-packages directory, and only
that virtual environment will be able to see them. Creating a new virtual environment each
time you start a project is a good habit to get into, because it’ll greatly simplify the
process of installing and managing Python code.

No comments:

Post a Comment