9. virtualenv
From this chapter, we'll do a review what we've done with "Hello World" tutorials. But it may not exactly a review of the same things since we'll do some new stuffs with a bit more complicated sample.
First, we need to get setuptools. It is a library designed to facilitate packaging Python projects, where packaging includes:
- Python package and module definitions
- Distribution package metadata
- Test hooks
- Project installation
- Platform-specific details
- Python 3 support
$ sudo apt-get install python-setuptools
Virtualenv is to create isolated environments, each running their own versions of packages. It is the tool in Python which helps in creating new virtual environments for our projects. Since it has its own install directories, we can be isolated from the system directories. We can even create virtual environments with various python versions, with each virtual environment having its own set of packages.
To install it, we do:
$ sudo easy_install virtualenv
Now we want to create a virtual environment by running the simple command with the following syntax:
virtualenv <environment_name>
OK, let's create it:
$ virtualenv --no-site-packages DjangoVirtualenv New python executable in DjangoVirtualenv/bin/python Installing setuptools, pip...done.
We used the --no-site-packages since we want to isolate our environment from the main site packages directory. Note that we can --system-site-packages instead if we do not want the isolation from the system packages. In other words, we can specify whether or not we want to inherit all the packages installed in our system.
$ source DjangoVirtualenv/bin/activate (DjangoVirtualenv)k@HP-Notebook:~/Django$
The prompt shows we are now in virtualenv. Actually, it let us know that we're running the proper virtualenv install. To deactivate, we can just run the following to come out of the environment:
$ deactivate
But we're not going to do that now.
If we go into the folder:
~/Django$ cd DjangoVirtualenv ~/Django/DjangoVirtualenv$ ls bin include lib local ~/Django/DjangoVirtualenv$ tree -L 2
It looks like the folder in our root file system, but it's not. It's a separate folder.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization