views:

884

answers:

4

I'm just beginning Python, and I'd like to use an external RSS class. Where do I put that class and how do I import it? I'd like to eventually be able to share python programs.

+1  A: 
from [module] import [classname]

Where the module is somewhere on your python path.

Andy Hume
I'd word that slightly differently. "python path" makes me think PYTHONPATH the environment variable. PYTHONPATH is actually normally empty, but sys.path is never empty, and sys.path is what determines where modules can be found. PYTHONPATH is just what's added to the default sys.path entries.
Devin Jeanpierre
+1  A: 

About modules and packages:

  • a module is a file ending with .py. You can put your class in such a file. As said by Andy, it needs to be in your python path (PYTHONPATH). Usually you will put the additional module in the same directory as your script is though which can be directly imported.

  • a package is a directory containing an __init__.py (can be empty) and contains module files. You can then import a la from <package>.<module> import <class>. Again this needs to be on your python path.

You can find more in the documenation.

MrTopf
Use `backticks` to quote formatting characters, `__init__` for example.
gahooa
Oversimplification. A .py file is not the only kind of module, and a package /is/ a module. A module is a type of python object. .py files and packagse are two standard ways of creating modules, as are .pyw, .pyd, .pyc, .pyo, and even other file formats/extensions with import hooks.
Devin Jeanpierre
thanks, gahooa. It also seems to work with a backslash when no backticks are there (which I added afterwards)Devin: That's why I linked to the docs for more detailed information.
MrTopf
+6  A: 

About the import statement:

(a good writeup is at http://effbot.org/zone/import-confusion.htm and the python tutorial goes into detail at http://docs.python.org/tutorial/modules.html )

There are two normal ways to import code into a python program.

  1. Modules
  2. Packages

A module is simply a file that ends in .py. In order for python, it must exist on the search path (as defined in sys.path). The search path usually consists of the same directory of the .py that is being run, as well as the python system directories.

Given the following directory structure:

myprogram/main.py
myprogram/rss.py

From main.py, you can "import" the rss classes by running:

import rss
rss.rss_class()

#alternativly you can use:
from rss import rss_class
rss_class()


Packages provide a more structured way to contain larger python programs. They are simply a directory which contains an __init__.py as well as other python files.

As long as the package directory is on sys.path, then it can be used exactly the same as above.


To find your current path, run this:

import sys
print(sys.path)
gahooa
+2  A: 

I don't really like answering so late, but I'm not entirely satisfied with the existing answers.

I'm just beginning Python, and I'd like to use an external RSS class. Where do I put that class and how do I import it?

You put it in a python file, and give the python file an extension of .py . Then you can import a module representing that file, and access the class. Supposing you want to import it, you must put the python file somewhere in your import search path-- you can see this at run-time with sys.path, and possibly the most significant thing to know is that the site-packages (install-specific) and current directory ('') are generally in the import search path. When you have a single homogeneous project, you generally put it in the same directory as your other modules and let them import each other from the same directory.

I'd like to eventually be able to share python programs.

After you have it set up as a standalone file, you can get it set up for distribution using distutils. That way you don't have to worry about where, exactly, it should be installed-- distutils will worry for you. There are many other additional means of distribution as well, many OS-specific-- distutils works for modules, but if you want to distribute a proper program that users are meant to run, other options exist, such as using py2exe for Windows.

As for the modules/packages distinction, well, here it goes. If you've got a whole bunch of classes that you want divided up so that you don't have one big mess of a python file, you can separate it into multiple python files in a directory, and give the directory an __init__.py . The important thing to note is that from Python, there's no difference between a package and any other module. A package is a module, it's just a different way of representing one on the filesystem. Similarly, a module is not just a .py file-- if that were the case, sys would not be a module, since it has no .py file. It's built-in to the interpreter. There are infinitely many ways to represent modules on the filesystem, since you can add import hooks that can create ways other than directories and .py files to represent modules. One could, hypothetically, create an import hook that used spidermonkey to load Javascript files as Python modules.

Devin Jeanpierre