Installing a python module with setup.py

Using setup.py to install a python module

Most larger packages (modules) give you an installer of some sorts that takes care of copying the needed files into the python system folders. Smaller packages may require you to run a python script (setup.py) for this. There's also a package called easy_install that may work for you, which I'll deal with after describing how setup.py works

On windows, there's a simple trick for installing pywapi, which is a super simple module consisting of just one file pywapi.py, try this first:

  1. get only  the file pywapi.py from the lecture notes folder 
  2. Find this folder:  c:\Python27\Lib\site-packages\   
  3. copy pywapi.py in to the c:\Python27\Lib\site-packages\ folder
  4. in a python shell type:
    import pywapi; dir(pywapi)

If this works, you've got pywapi installed!

The following is still usefull if you want to learn how to use setup.py to install pywapi or any other Python modules. Mac users, see the On Mac: part below

I'll walk you through setup.py-installing a small module (pywapi) which grabs local weather data from various web services (Google, Yahoo and NOAA).  Get 0.0.2 from the official google code download link or from the lecture notes and unzip it to a temp folder, such as C:\temp (Windows) or temp in your home folder (Mac),

On Windows:
This type of "zip" file happens to be a .tar.gz file which Windows may not recognize as a "proper" zip file. In this case, install 7-zip (opensource version of zip) which will be able to deal with .tar.gz and will also read/write "normal" .zip files.

But, I've also made a regular zip version for you (pywapi-0.2.2,zip) and put it into the lecture notes.

Note: Some programs (winzip?) will unzip a .tar/gz file into a .tar file, not a folder. You may have to unzip that again to get a real folder. Or use the .zip file I created instead the official .tar.gz file.

After you have unzipped the .tar.gz file in a folder (should be pywapi-0.2.2), note that folder's path. Bring up a terminal (cmd.exe) and "go" (cd) into that  folder (should be in  c:\temp\pywapi-0.2.2 or wherever you unzipped into) by typing cd c:\temp\pywpis-0.2.2\  at the console prompt. Now start a python shell to run setup.py file, first with build as argument and then with install:

(Here's how Nandu got it to work in Windows 7, unzipping it to c:\pywapi-0.2.2 instead of c:\temp\pywapi-0.2.2 ...)

Test if import pywapi works, you should see something when you use dir(pywapi). BTW the build part creates a bunch of .pyc (bytecode) files and install copies them into C:\PythonXX\Lib\site-packages. You could remove the c:\temp\pywapi-0.2.2 folder after everything works, but take note of the examples in its examples folder first, these will typically not be installed automatically, so you may want to look at them first ...

python.exe cannot be not found ('python' is not recognized as a command - error)
Some students have reported that the python executable (python.exe) is not found after they open a terminal (cmd.exe) and type in pythonThat usually has to do with the executable for the Python interpreter (python.exe) not being found when the list of folders with executables is traversed. This list is stored in a system wide environment variable called path. To fix this add  C:\Python2.6 to this system environment variable. Here's a link to the python online doc http://docs.python.org/using/windows.html - Setting environment variables
Vanessa also found a video for setting this "DOS" path for the python executable: Adding Python to DOS Path 

Note that you should only append (add) the path of the python folders (;C:\Python26\;C:\Python26\scripts\;) to the end of your already defined path, don't overwrite what you already have there now or other executables may not work anymore! To test this, in cmd type set path, this shows the current path and it should contain something  like C:\Python2.6.  If this is true, the executable for the python shell (python.exe) should run when you type python inside a terminal. Now go back and try to install pywapi by running setup.py.

-----------------------------------------------

This way of installing a module is pretty standard and uses a python script to install an new module. Most bigger modules have a nice installer, though and I looked at the distutil moduleon how to create installers for pywapi. That seems to have worked for Windows (32 bit), which is good because some Windows installations of Python don't automatically put the python executable in the path, so can't use setup.py in cmd and need to fix the path manually first. So, if you have trouble with setup.py on Windows, try pywapi-0.2.2.win32-py2.6.msi (not sure it that works for all version of Windows, though).

Most (all?) Mac Python's seem to add that automatically at install, and you should be able to run setup.py in the terminal(.app) as described above. So, here are my alternatives to the official pywapi-0.2.2.tar.gz you can download from Google Code, the pywapi folder inside the lecture08 notes contain all of the following installers (in addition to pywapi-0.2.2.tar.gz):
  • pywapi-0.2.2.zip - I just created a zip file from pywapi-0.2.2.tar.gz, it still needs to be installed via setup.py
  • pywapi-0.2.2.win32-py2.6.msi (32-bit) - real installer for Windows, will guide you through installing (copying) a couple of files into your current site-packages folder, after that you should be able to import pywapi (made with python setup.py bdist_msi on Windows 7, 32 bit).
  • pywapi-0.2.2.macosx-10.6-universal.tar.gz - a OS X package I made but it's not a real installer. Unzipping it creates a Library Folder with sub (sub) folders. In Library, drill down into the last subfolder, which will contain 3 files: pywapi-0.2.2-py2.6.egg-info, pywapi.py and pywapi.pyc). Copy those files into your /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages folder (your official 3.party module folder which python knows about already). After that you should be able to import pywapi. (I made this package via python setup.py bdist on Snow Leopard)


On Mac:
First check if you have something called easy_install on your Mac (most newer Macs should). Open a terminal (Applications/Utilities or spotlight terminal) and type:


easy_install

if you get this back:

error: No urls, filenames, or requirements specified (see --help)


you're ok, you have it installed! Now you can tell easy_install to fetch and install pywapi for you!
Try this:

easy_install pywapi


here's what happens on my OS 10.6 (Snow leopard) Mac:


Searching for pywapi
Reading http://pypi.python.org/simple/pywapi/
Reading http://code.google.com/p/python-weather-api/
Best match: pywapi 0.2.2
Downloading http://python-weather-api.googlecode.com/files/pywapi-0.2.2.tar.gz
Processing pywapi-0.2.2.tar.gz
Running pywapi-0.2.2/setup.py -q bdist_egg --dist-dir /var/folders/rH/rH8MpZSiGnOhUqJbPUEOlE+++TI/-Tmp-/easy_install-LSGQQv/pywapi-0.2.2/egg-dist-tmp-dsWKFx
zip_safe flag not set; analyzing archive contents...
Adding pywapi 0.2.2 to easy-install.pth file
Installed /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pywapi-0.2.2-py2.6.egg

Processing dependencies for pywapi
Finished processing dependencies for pywapi

If you're on Lion (10.7), you may need to run easy_import in the so-called super-user mode. Put sudo (as super-user do the following ...) in front of easy_install:

sudo easy_install pywapi

Now, type in your password to give Lion permission to install pywapi and it would work.


What if you don't have easy_install?
Get the the zipped pywapi package from the official google code download link or from the lecture notes (pywapi-0.2.2.tar.gz The internal unzip application should be able to deal with .tar.gz files. Move the unzipped folder into a temp folder in you home folder (e.g. \Users\chris\temp) and open a terminal (Applications/Utilities or spotlight terminal). This will automatically put you in your home folder (\Users\chris), cd into temp and then into the pywapi-0.2.2 folder. Run the setup.py, this should create a .pyc file and put it and some other pywapi files into you site-packages folder (for me, on Snow Leopard that's: /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages folde). Get a python shell and type import pywapi; dir(pywapi)  to test if pywapi has been installed correctly.


1 comment:

Unknown said...

Hi Professor Harding,
I found a great video link that explains how to change the path on windows. This helps wit the setup.py step.

The video url is:

http://showmedo.com/videos/video?name=960000&fromSeriesID=96

This is what I typed in to exectue Python from from any path on my machine.
\;c:\Python26;c:\Python26\scripts

Search This Blog