[Tutor] Installing module and running

Walter Prins wprins at gmail.com
Wed Jul 20 11:38:49 CEST 2011


Hi David,

On 19 July 2011 20:34, David Merrick <merrickdav at gmail.com> wrote:

> Thanks for you help.Its 64 bit windows. What directory would the module go
> in? Scripts or site-packages in Lib?
>
> I have Python 2.6 in C drive but the files I am using are in a directory on
> d drive.
>
> Does the the module have to be in in the same directory as the files that
> use it?
>
>
When you install additional packages into Python (for example, the "nose"
package as in your case), they become part of your Python installation.
Because of this, you can import them just like you can import any other
module that is part of Python already and in some cases they add new
scripts/commands that can be run from a command line.

Physically what happens during installation is that the .py files (and
folders/packages) get put into your "Python\Lib" folder (for example
"C:\Python27\Lib" on my machine.)  As an aside, the reason Python is able to
find modules in this folder automatically is that the Python\Lib folder is
(by default) one of the folders that Python searches in when it encounters
an import statement.  Consequently, when you put a python module in the
Python\Lib folder, it becomes effectively available to any other Python
script you might want to run regardless of where the script being run is
physically located.

As mentioned previously, Python packages oftentimes also include command
line programs to interface with the Python installation, or provide other
functionality.  Typically such modules/programs are put into the
"Python\Scripts" folder, the idea being that you as a user can then run
these programs from there.  Unfortunately by default (at least on Windows),
the "Python\Scripts" folder is not on your command line (system) path and so
won't be found if you type only the script/command name at the command
prompt.  Consequently, if you want to use a Python script installed in the
Python\Scripts folder, you have to make that the current working directory
before running the script or specify the full path to the script, otherwise
the system obviously won't know where to find the script to run and will
respond with a "Bad command or filename" response.

What you want to do is to add the "Python\Scripts" folder to your system
path, then you'll be able to run these Python scripts/commands as if they're
inbuilt operating system commands without having to either be in the
Python\Scripts folder or specify the full path to run them.  To do this,
click on the "Start" button, right click "Computer", click "Properties",
click "Advanced System settings", click "Environment variables" button
below, find the "PATH" entry in the "System variables" list and click it to
select it, then click "Edit" below that list, press "End" to move to the end
of the line and append the scripts folder with a semicolon to delimit it
from the previous path.  In my case I therefore would append (excluding the
quotes): ";c:\Python27\Scripts" Click OK, OK again, OK again. Now when you
open a command prompt any script inside of the Python\Scrtips folder will be
directly runnable.

Now about Python package installation:  By convention if you download a
Python package manually, you can manually install it by extracting the
package archive, then changing the working directory to the root of
extracted package, and then running the "setup.py" script that is usually
included with Python packages, with an "install" option, e.g.:

python setup.py install

This does the donkey work to copy the modules of the package into the
Python\Lib folder, and Python\Scripts folder, if relevant, plus anything
else that might need to be done.  You could install your nose package like
this, e.g. by downloading it, extracting it, then running the above command
in the root of the nose folder.

I'd like to reiterate though, that one of the packages that make the
installation other Python packages even easier, is the "setuptools" package,
which amongst other things creates the "easy_install" script under the
Python\Scripts folder.  What it does, is to allow you to install other
Python packages directly from the Python package archive without having to
manually download them.  Eg once you have setuptools installed and thus have
easy_setup available as a command/script (runnable from anywhere if you've
updated your system PATH as above), then you can install nose (or any other
common package available in the Python package repository) by simply opening
a command prompt and issuing:

easy_install <packagename>

So in your case you'd enter:

easy_install nose

That's it!  This will then automatically download the nose package from the
internet, and install it for you. It really is worth it to install
setuptools (or one of the newer variants like "pip" or "distribute"...) as
this makes your life as a Python developer a lot easier when it comes to
getting packages downloaded and installed into your Python environment.

Does that give you enough understanding to get you going?

Cheers

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110720/b5e645a5/attachment.html>


More information about the Tutor mailing list