External classes in Python

0 votes
asked Feb 28, 2009 by user29772

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.

6 Answers

0 votes
answered Jan 28, 2009 by andy-hume
from [module] import [classname]

Where the module is somewhere on your python path.

0 votes
answered Jan 28, 2009 by mrtopf

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.

0 votes
answered Jan 28, 2009 by devin-jeanpierre

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.

0 votes
answered Feb 28, 2009 by gahooa

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)
0 votes
answered Jan 4, 2013 by joe

If you want to store your RSS file in a different place use sys.append("") and pout the module in that directory and use import or from import *

0 votes
answered Sep 15, 2017 by davide-damico

The first file, where you have created the class, is "first.py"

first.py:

class Example:
   ...

You create the second file, where you want to use the class contained in the "first.py", which is "second.py"

myprogram/first.py
myprogram/second.py

Then in the second file, to call the class contained in the first file, you simply type:

second.py:

from first import Example
...
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...