Skip to content

Triangle Python Users Group: TriPython (formerly TriZPUG)

Sections
Personal tools
You are here: Home » Boot Camps » Seattle PyCamp 2011 » Installing Setuptools and Virtualenv for PyCamp

Installing Setuptools and Virtualenv for PyCamp

Document Actions
We can easily install third-party software, including your own, into your new Python 2.6.6 with the Setuptools package. However, uninstalling software packages is not always straightforward. Third-party software varies greatly in quality. And as new Python programmers, you will make many mistakes in your software packages as first. You could end up with a Python polluted full of junk software packages if you aren't very careful, probably more careful than you know how to be at the moment. Fortunately, Python provides you with a way to create throw-away "virtual private Pythons" into which you can install third-party software of unknown quality, test it, throw the virtual Python away when you are satisfied, and start over with as many clean new virtual Pythons as you like. This procedure gets you set up to do just that.

You must complete the PyCamp Python Installation Procedure prior to attempting the procedure on this page.

You must be connected to the Internet in order to complete this procedure.

Contents

OSX
Windows
Linux

Installing Setuptools and Virtualenv on OSX 10.3 through 10.6

If you have not already installed the Xcode disc which came with your OSX, you should do so before performing any of the rest of this procedure.

Pick your OSX version:
Snow Leopard (OSX 10.6)
Leopard (OSX 10.5) or less

Installing Setuptools and Virtualenv on Snow Leopard (OSX 10.6)

  1. If you installed Python 2.6.6 on Snow Leopard instead of sticking with the Python 2.6.1 which comes with Snow Leopard, then you should use the Installing Setuptools and Virtualenv on OSX 10.5 procedure instead.
  2. You are luckiest of all. easy_install comes with Snow Leopard. All that is left for you to do here is to use easy_install to install virtualenv. The easy_install which comes with Snow Leopard installs packages to /Library/Python/2.6/site-packages.
  3. Look in Finder under Applications->Utilities and start Terminal.app.
  4. By the way, your PyCamp instructor recommends iTerm as a most excellent free and open source alternative to Terminal.app. Your PyCamp instructor wouldn't think of using another terminal besides iTerm. Two words: tabbed terminals.
  5. Issue the command sudo easy_install virtualenv | tee ~/virtualenv.stdout. Supply your login password when asked. This will allow easy_install to install the virtualenv binary to /usr/local/bin and the rest of the virtualenv package and its dependencies to /Library/Python/2.6/site-packages. Several diagnostic messages will fly by before you are returned to the terminal prompt.
  6. /usr/bin/local should already be in your $PATH environment variable, so you should now be able to test out your new virtualenv command.
  7. At the terminal prompt, issue the command virtualenv --no-site-packages ~/mypy | tee ~/mypy.stdout. Several diagnostic messages will fly by before you are returned to the terminal prompt. Congratulations. Your own virtual private Python awaits you.
  8. At the terminal prompt, change directories into your new virtual private Python installation directory: cd ~/mypy.
  9. Try it out: bin/python.
  10. At the >>> Python prompt, see that your virtual Python really is private by examining the output of: import sys; from pprint import pprint; pprint(sys.path).
  11. Exit your virtual Python the normal way by typing ctrl-D.
  12. You can make your virtual Python the default Python for your current terminal session by issuing the command source bin/activate. You should notice your terminal prompt change to show you that your virtual Python has been temporarily activated.
  13. As long as your virtual Python is activated, you only have to enter python at the terminal prompt to start it instead of the system Python. In fact, your can start any command in your virtual private Python's bin directory while your virtual private Python is activated without having to prefix the command with bin/.
  14. Your virtual private Python has it's own site-packages directory separate from your system Python's site-packages directory. Anything you install to your virtual private Python will have no affect on your system Python. There is even an easy_install already in your virtual private Python's bin/ directory, ready and waiting for you to install packages from the Python Package Index (Pypi) that only your virtual private Python will know about.
  15. At any time, issue the command deactivate to go back to using your system Python, system easy_install, and system site-packages directory.
  16. If you exit your terminal session, your virtual private Python will be automatically deactivated. Any other terminal sessions you start simultaneously will only use your virtual private Python if you activate it in each termianl session you wish to use it in.
  17. After you have deactivated your virtual Python, change directories out of it (cd ~) and delete your virtual private Python: rm -rf ~/mypy. You may create and throw away virtual private Pythons as often as you wish. You may also have several virtual private Pythons at once, all with different sets of third-party Python packages installed from Pypi.
  18. To keep up with his sometimes many virtual private Pythons, your instructor tends to create them all in a subdirectory called Environments in his home directory.
  19. You will be shown in class where to create a virtual private Python for the class exercises.

Installing Setuptools and Virtualenv on OSX 10.5 or less (but at least 10.3)

  1. Download the Setuptools Bootstrap Script to your home directory. The best way to do this is at a terminal prompt. Issue the command curl http://peak.telecommunity.com/dist/ez_setup.py > ~/ez_setup.py.
  2. At the terminal prompt, run the bootstrap script with the python you installed for PyCamp: python ~/ez_setup.py | tee ~/ez_setup.stdout. Several diagnostic messages will fly by before you are returned to the terminal prompt.
  3. Verify that easy_install was installed in the correct location and is now the default easy_install by issuing the command which easy_install. The answer you get should be /Library/Frameworks/Python.framework/Versions/2.6/bin/easy_install. If so, you are now ready to use your new easy_install Python package manager to install virtualenv. Your new easy_install installs packages to /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages.
  4. Issue the command easy_install virtualenv | tee ~/virtualenv.stdout. Several diagnostic messages will fly by before you are returned to the terminal prompt.
  5. Verify that virtualenv was installed in the correct location and is now the default virtualenv by issuing the command which virtualenv. The answer you get should be /Library/Frameworks/Python.framework/Versions/2.6/bin/virtualenv.
  6. At the terminal prompt, issue the command virtualenv --no-site-packages ~/mypy | tee ~/mypy.stdout. Several diagnostic messages will fly by before you are returned to the terminal prompt. Congratulations. Your own virtual private Python awaits you.
  7. At the terminal prompt, change directories into your new virtual private Python installation directory: cd ~/mypy.
  8. Try it out: bin/python.
  9. At the >>> Python prompt, see that your virtual Python really is private by examining the output of: import sys; from pprint import pprint; pprint(sys.path).
  10. Exit your virtual Python the normal way by typing ctrl-D.
  11. You can make your virtual Python the default Python for your current terminal session by issuing the command source bin/activate. You should notice your terminal prompt change to show you that your virtual Python has been temporarily activated.
  12. As long as your virtual Python is activated, you only have to enter python at the terminal prompt to start it instead of the Python 2.6.6 you so carefully installed and wish to protect. In fact, your can start any command in your virtual private Python's bin directory while your virtual private Python is activated without having to prefix the command with bin/.
  13. Your virtual private Python has it's own site-packages directory separate from the site-packages directory of the Python you installed (called the "parent" Python of the virtual private Python). Anything you install to your virtual private Python will have no affect on your parent Python. There is even an easy_install already in your virtual private Python's bin/ directory, ready and waiting for you to install packages from the Python Package Index (Pypi) that only your virtual private Python will know about.
  14. At any time, issue the command deactivate to go back to using your parent Python, parent easy_install, and parent site-packages directory.
  15. If you exit your terminal session, your virtual private Python will be automatically deactivated. Any other terminal sessions you start simultaneously will only use your virtual private Python if you activate it in each termianl session you wish to use it in.
  16. After you have deactivated your virtual Python, change directories out of it (cd ~) and delete your virtual private Python: rm -rf ~/mypy. You may create and throw away virtual private Pythons as often as you wish. You may also have several virtual private Pythons at once, all with different sets of third-party Python packages installed from Pypi.
  17. To keep up with his sometimes many virtual private Pythons, your instructor tends to create them all in a subdirectory called Environments in his home directory.
  18. You will be shown in class where to create a virtual private Python for the class exercises.

Installing Setuptools and Virtualenv on Windows 2000 through Windows 7

  1. Log into Windows as a user with administrative privileges on the laptop (i.e., you are able to install programs). You will need to use this user in class as well. Don't come to class without access to a user with administrative privileges. If you own your own laptop, don't worry, your userid already has administrative privileges. If you are bringing a laptop from work, and only someone else has the authority to install new programs on that laptop, you will need to speak with that person and talk some sense into them.
  2. Download the Setuptools Bootstrap Script to your desktop by right clicking on the link, clicking on "Save Link As" (Firefox) or "Save Target As" (Internet Explorer), navigating to "Desktop" on the "Save As" dialog, selecting "All Files" on the "Save As Type" drop-down menu, and finally clicking "Save" (and then "Close" when the download has completed).
  3. Click through Start -> Programs -> Accessories -> Command Prompt to start a command prompt. Be sure and right click on the Command Prompt icon and select "Run as Administrator" when you start a command prompt. If at any time you want to end your command prompt, you may simply type exit and press Enter and the command prompt.
  4. Change directories to the root of your Desktop (substituting "yourusername" with your actual Windows user name):
    • On Windows 7 and Vista type: cd \Users\yourusername\Desktop and press Enter.
    • On Windows XP and 2000 type: cd "\Documents and Settings\yourusername\Desktop" and press Enter (don't forget the double quotes; they are important).
    Always be sure you have changed directories to your Desktop before running any of the following commands. You can always check what your current directory is by typing cd and pressing Enter at the command prompt.
  5. At the command prompt, enter the command to run the downloaded script with the Python you installed for class: \Python26\python.exe ez_setup.py > ez_setup.stdout. The command will take several seconds as it downloads the setuptools package and installs it.
  6. When you are returned to the command prompt, you can check the results of running ez_script.py by looking at the saved output: type ez_setup.stdout.
  7. Enter the command dir \Python26\Scripts to see that a new program called easy_install.exe has been installed for you.
  8. Let's run easy_install to download and install the virtualenv package from python.org. It's easy. Just enter \Python26\Scripts\easy_install.exe virtualenv > virtualenv.stdout. The command will take several seconds as it downloads the virtualenv package and installs it.
  9. When you are returned to the command prompt, you can check the results of using easy_install by looking at the saved output: type virtualenv.stdout.
  10. Enter the command dir \Python26\Scripts to see that a new program called virtualenv.exe has been installed for you.
  11. Let's use virtualenv to create your own virtual private Python. You may create as many virtual private Pythons as you want. You may have several virtual private Pythons at once. Each one has its own easy_install program and each one has its own site-packages directory where easy_install installs third-party Python packages from the Python Package Index (Pypi).
  12. To create a virtual private Python called mypy on your Desktop, enter the command \Python26\Scripts\virtualenv.exe --no-site-packages mypy > mypy.stdout. The command takes just a few seconds to run.
  13. When you are returned to the command prompt, you can check the results of using virutalenv by looking at the saved output: type mypy.stdout.
  14. Let's use your new virtual private Python. To make it easier, let's change directories into your new private Python: cd mypy.
  15. Once in the mypy directory, Scripts\python.exe starts your virtual private Python.
  16. See for yourself that this Python really is private by entering the following command at the >>> Python prompt: import sys; from pprint import pprint; pprint(sys.path).
  17. Exit your virtual private Python by typing ctrl-Z and then pressing Enter.
  18. Enter Scripts\activate.bat at your command prompt once you have exited Python. This "activates" your virtual private Python. You see this by a change in your command prompt. Now when you simply enter python or easy_install or the name of any executable in your virtual private Python's Scripts directory, you will run that command from your virtual private Python. This cuts down on confusion about which Python you are using or installing packages to.
  19. Let's install a third-party Python package from Pypi into your activated virtual private Python: easy_install yolk. Several diagnostic messages will fly by before you are returned to the command prompt.
  20. Now the yolk package is installed in your virtual private Python, but not in the "parent" Python you installed at \Python26. Installing the yolk package also created a new program called yolk.exe in your virtual private Scripts directory.
  21. The yolk package can do many things. But one of the most useful things it can do is show you a convenient listing of all the third-party Python packages you have installed and which versions of them are installed. Do that now by entering the command yolk --list. You should see at least pip, setuptools, and yolk itself installed.
  22. When you are done playing around with your virtual private Python, it is time to "deactivate" it. Do so by entering deactivate.bat. Your command prompt is returned to its normal state.
  23. Change directories out of your virtual private Python: cd .. (this should return you to your Desktop directory, which is the parent of your mypy directory). Now delete your virtual private Python: del mypy /S /Q. You can always create another virtual private Python later.
  24. You will be shown in class where to create a virtual private Python for the class exercises.

Installing Setuptools and Virtualenv on Linux

Pick the method by which you installed Python 2.6:
My Linux came with Python 2.6 installed
I got Python 2.6 from my Linux repository
I compiled Python 2.6 from source code

Installing Setuptools and Virtualenv on a Built-in Python 2.6

  1. You are tremendously lucky. In all great probability, setuptools and virtualenv can be installed all at once from your Linux repository. In you Linux repository package manager, search for virtualenv and install it. Installing the virtualenv package will automatically pull in prerequisites like setuptools. That's it, you are done.
  2. If you don't find virtualenv in your Linux repository using your package manager, you can use the method of Installing Setuptools and Virtualenv on a Python 2.6 Installed From Scratch by changing /opt/python26/ to /usr/bin/ in the instructions.

Installing Setuptools and Virtualenv on a Python 2.6 Installed from a Linux Distribution Repository

  1. There is a chance setuptools and virtualenv can be installed from your Linux repository. In you Linux repository package manager, search for virtualenv-2.6 and install it. Installing the virtualenv package will automatically pull in prerequisites like setuptools-2.6. That's it, you are done.
  2. If you don't find virtualenv-2.6 in your Linux repository using your package manager, you can use the method of Installing Setuptools and Virtualenv on a Python 2.6 Installed From Scratch by changing /opt/python26/ to /usr/bin/ in the instructions.

Installing Setuptools and Virtualenv on a Python 2.6 Installed From Scratch

  1. Download the Setuptools Bootstrap Script to your Python installation: cd /opt/python26; sudo wget http://peak.telecommunity.com/dist/ez_setup.py.
  2. Use your compiled Python to run the script: sudo bin/python ez_setup.py | sudo tee ez_setup.stdout.
  3. Use your new setuptools package to install virtualenv by issuing the command sudo bin/easy_install virtualenv | sudo tee virtualenv.stdout.
  4. Use virtualenv to create a virtual private Python in your home directory: bin/virtualenv --no-site-packages ~/env/mypy.
  5. Change directories to your new virtual private Python: cd ~/env/mypy.
  6. "Activate" your new virtual private Python: source bin/activate. You should notice your terminal prompt change to show which Python is "active."
  7. Install a third-party Python package from the Python Package Index (Pypi) into your new virtual private Python: easy_install yolk.
  8. Use yolk to look over the third-party Python packages and their versions installed in your virtual private Python: yolk --list.
  9. "Deactivate" your virtual private Python: deactivte. See your terminal prompt go back to normal.
  10. Hop up on directory: cd ..
  11. Delete your virtual private Python: rm -rf mypy. You can always create more later.
  12. You will be shown in class where to create a virtual private Python for the class exercises.