Virtualenv and Virtualenvwrapper on Ubuntu 14.04

Source: Internet
Author: User
Tags virtual environment virtualenv

In this post I'll go over my attempt to setup virtual environments for Python development. Most Python users probably don ' t want the use of virtual environments and should just set up a single user environment that Wo Rks for their needs. However, if you is working on writing one or more Python modules or packages, these tools is geared towards creating ISO lated Python environments for each project. This was very useful for keeping track of such things as the minimal Python requirements for each project.

Assuming want to proceed, my goal are to setup and Usevirtualenvwrapper (see Virtualenvwrapper docs for more info), a s Et of shell tools wrapped around the virtualenv package. Both the Doug Hellman post and the Simonsoftware post provide some motivation for the development and use of the wrapper F Ormulation–in Simple Termsvirtualenvwrapper provides some shortcuts for common actions. So, I ' ve decided to use the wrapped version.

Basic Install

Following the Virtualenvwrapper basic install, the installation process is to install virtualenv and Virtualenvwrapper usi ng Pip. I have already installed virtualenv, as can is seen by using pip Show:

$ pip Show virtualenv---name:virtualenvversion:1.11.6location:/home/cstrelioff/.local/lib/python2.7/ Site-packagesrequires:

If you need to install, the command is–i install as a user:

$ pip Install--user virtualenv

Next, install the Virtualenvwrapper:

$ pip Install--user Virtualenvwrapper

Now a pip Show should show something like:

$ pip Show Virtualenvwrapper---name:virtualenvwrapperversion:4.3.1location:/home/cstrelioff/.local/lib/python2.7/ Site-packagesrequires:virtualenv, Virtualenv-clone, stevedore

Finally, we add some information to our ~/.BASHRC File–i added to the end of the file, as usual for such things. The actual contents for you would be different.

    • I wanted my virtual environments in ~/virtenvs/and I had already made that directory.
    • I want to keep my active projects in ~/projects-active/, an existing directory.
    • Finally, because I installed as a user, the path to myvirtualenvwrapper.sh are in ~/.local/bin/, as indicated b Y usingwhich:
$ which virtualenvwrapper.sh/home/cstrelioff/.local/bin/virtualenvwrapper.sh

Putting all of this specific information together, I added the following to ~/.BASHRC:

# Where to store our virtual Envsexport workon_home= $HOME/virtenvs# where projects would resideexport project_home= $HOME/PR ojects-active# where is the Virtualenvwrapper.shsource $HOME/.local/bin/virtualenvwrapper.sh

After saving the changes, I sourced the file to make the changes active:

SOURCE ~/.BASHRC
Working with Virtualenvs

Next up, let's figure out what to do with all this–you should also look at the Simonsoftware post for another take in this. The main command to remember was Workon, as in I ' m goingto work on this project. However, if we try it now we get nothing:

$ workon$

We need to make a virtual environment. So, let's make one:

$ mkvirtualenv test_env01new python executable in test_env01/bin/pythoninstalling setuptools, Pip...done.

We can use Pips list to see the packages available:

(Test_env01)(1.2.1 (1.5.6 (3.6 (0.1.2)     

Notice that the command prompt have changed to include the environment name. If we want to install a package in this environment we use pip:

(Test_env01)$ pip Install Pyaml 

Now, a pip List gives:

(Test_env01)(1.2.1 (1.5.6 (14.05.7    (3.11 (3.6) 

To deactivate the virtual environment, we type exactly "what do you ' d expect:

(TEST_ENV01) $ deactivate$

And we get back to the normal command prompt. However, now theWorkon command would show the virtual environment that we created:

$ workontest_env01$

To start working in it again, simply try out the following to see everything is there:

$ Workon test_env01 (test_env01)(1.2.1(1.5.6(14.05.7 (3.11) (3.6 (0.1.2) ( TEST_ENV01)$ deactivate           
Projects in Virtualenvwrapper

Finally, let's talk about the projects in Virtualenvwrapper. This creates both (i) a virtual environment and (ii) a project directory in the location specified by project_home var Iable in the additions to the~/.BASHRC file. Let's try it out:

$ mkproject test_project02new python executable in test_project02/bin/pythoninstalling setuptools, Pip...done. Creating/home/cstrelioff/projects-active/test_project02setting Project for test_project02 to/home/cstrelioff/ projects-active/test_project02 (test_project02) ~/projects-active/test_project02$

Notice that this also creates a directory and CD's to Directory–very nice! Now, if we try to pip List We ll see only the packages for a new environmnet:

(test_project02) ~/projects-active/test_project02 (1.2.1 (1.5.6 (3.6)  
Switching between environments

Now we have the virtual environments Setup, but only one is setup as a project. We can see both with Workon:

$ workontest_env01test_project02

To get a sense of how the this all works, let startup test_env01 and use pips list to see that Pyyaml is install Ed:

$ Workon test_env01 (test_env01)(1.2.1(1.5.6(14.05.7(3.11) (3.6 (0.1.2) 

Next, while in test_env01, let's switch to test_project02 using Workon and look at the installed packages ( No pyyaml):

(Test_env01)$ workon test_project02(test_project02) ~/projects-active/test_project02(1.2.1 (1.5.6 (3.6(0.1.2)        

Notice that the Workon CD's to the project directory. This happens because we setup test_project02 as a project and not just avirtualenv. If you use Workon-to-switch back to test_env01 there'll be no CD because there is no project file Ociated with that virtual environment. In practice I imagine I'll always use mkproject to set things up.

Cleaning up

Finally, to the example above we can use rmvirtualenv:

$ workontest_env01test_project02$ rmvirtualenv test_env01removing test_env01...$ rmvirtualenv test_project02Removing test_project02...$ workon$

With the final Workon we can see, all of our environments is gone. However, note that the directory created in Project_homewill isn't being deleted by the Above–this is probably a good DEFA Ult behaviour. You'll have to go delete the directory (if you want).

That ' s it, hopefully some'll find this useful post useful. If you had cool/better ways to use these tools leave a comment below.

Virtualenv and Virtualenvwrapper on Ubuntu 14.04

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.