|
|
Installing Setuptools and Virtualenv for PyCamp
We can easily install third-party software, including your own, into your new Python 2.6.5 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.
OSX
Windows
Linux
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
- If you installed Python 2.6.5 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.
- 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.
- Look in Finder under Applications->Utilities and start Terminal.app.
- 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.
- 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.
/usr/bin/local should already be in your $PATH environment variable, so you should now be able to test out your new virtualenv command.
- 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.
- At the terminal prompt, change directories into your new virtual private Python installation directory:
cd ~/mypy.
- Try it out:
bin/python.
- 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).
- Exit your virtual Python the normal way by typing ctrl-D.
- 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.
- 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/.
- 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.
- At any time, issue the command
deactivate to go back to using your system Python, system easy_install, and system site-packages directory.
- 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.
- 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.
- 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.
- You will be shown in class where to create a virtual private Python for the class exercises.
- 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.
- 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.
- 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.
- Issue the command
easy_install virtualenv | tee ~/virtualenv.stdout. Several diagnostic messages will fly by before you are returned to the terminal prompt.
- 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.
- 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.
- At the terminal prompt, change directories into your new virtual private Python installation directory:
cd ~/mypy.
- Try it out:
bin/python.
- 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).
- Exit your virtual Python the normal way by typing ctrl-D.
- 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.
- 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.5 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/.
- 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.
- At any time, issue the command
deactivate to go back to using your parent Python, parent easy_install, and parent site-packages directory.
- 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.
- 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.
- 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.
- You will be shown in class where to create a virtual private Python for the class exercises.
Note: your instructor has not tested this procedure on Windows 7. Windows 7 is known to make a lot of non-Microsoft product installers blow up. You have been warned. As they say, untested code is broken code.
- 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.
- Download the Setuptools Bootstrap Script to the root of your C: drive by right clicking on the link, clicking on "Save Link As" (Firefox) or "Save Target As" (Internet Explorer), navigating through MyComputer -> Local Computer (C:) 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).
- Click through Start -> Programs -> Accessories -> Command Prompt to start a command prompt. Change directories to the root of your C: drive by typing the command
cd \ and the pressing Enter.
- 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.
- 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.
- Enter the command
dir \Python26\Scripts to see that a new program called easy_install.exe has been installed for you.
- 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.
- 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.
- Enter the command
dir \Python26\Scripts to see that a new program called virualenv.exe has been installed for you.
- 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).
- To create a virtual private Python called
mypy in the root of your C: drive, enter the command \Python26\Scripts\virtualenv.exe --no-site-packages \mypy > \mypy.stdout. The command takes just a few seconds to run.
- 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.
- Let's use your new virtual private Python. To make it easier, let's change directories into your new private Python:
cd \mypy.
- Once in the
\mypy directory, Scripts\python.exe starts your virtual private Python.
- 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).
- Exit your virtual private Python by typing ctrl-Z and then pressing Enter.
- 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.
- 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.
- 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.
- 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.
- 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.
- Change directories out of your virtual private Python:
cd \. Now delete your virtual private Python: del mypy /S /Q. You can always create another virtual private Python later.
- You will be shown in class where to create a virtual private Python for the class exercises.
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
- 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.
- 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.
- 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.
- 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.
- Download the Setuptools Bootstrap Script to your Python installation:
cd /opt/python26; sudo wget http://peak.telecommunity.com/dist/ez_setup.py.
- Use your compiled Python to run the script:
sudo bin/python ez_setup.py | sudo tee ez_setup.stdout.
- Use your new
setuptools package to install virtualenv by issuing the command sudo bin/easy_install virtualenv | sudo tee virtualenv.stdout.
- Use
virtualenv to create a virtual private Python in your home directory: bin/virtualenv --no-site-packages ~/env/mypy.
- Change directories to your new virtual private Python:
cd ~/env/mypy.
- "Activate" your new virtual private Python:
source bin/activate. You should notice your terminal prompt change to show which Python is "active."
- Install a third-party Python package from the Python Package Index (Pypi) into your new virtual private Python:
easy_install yolk.
- Use
yolk to look over the third-party Python packages and their versions installed in your virtual private Python: yolk --list.
- "Deactivate" your virtual private Python:
deactivte. See your terminal prompt go back to normal.
- Hop up on directory:
cd ..
- Delete your virtual private Python:
rm -rf mypy. You can always create more later.
- You will be shown in class where to create a virtual private Python for the class exercises.
|
This site conforms to the following standards:
|