Outer classes in Python

I am just starting out Python, and I would like to use an external RSS class. Where do I put this class and how to import it? I would ultimately want to be able to share python programs.

+4
source share
6 answers

About the import statement:

(a good entry is at http://effbot.org/zone/import-confusion.htm , and the python manual is described in detail at http://docs.python.org/tutorial/modules.html )

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

  • Modules
  • Packages

The module is simple - a file that ends with .py. For python, it must exist in the search path (as defined in sys.path). Usually the search path consists of the same directory as the .py being launched, as well as the python system directories.

Given the following directory structure:

 myprogram/main.py myprogram/rss.py 

From main.py you can "import" 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. This is just a directory containing __init__.py as well as other python files.

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


To find your current path, run this:

 import sys print(sys.path) 
+13
source

I do not really like to answer so late, but I am not completely satisfied with the existing answers.

I am just starting out Python, and I would like to use an external RSS class. Where do I put this class and how to import it?

You put it in a python file and give the python file a .py extension. Then you can import the module representing this file and access the class. Suppose you want to import it, you should put the python file somewhere in the import search path - you can see it at runtime with sys.path , and perhaps the most important thing to know is that the packages site (installation-specific), and the current directory ('') is usually located in the import search path. When you have one homogeneous project, you usually put it in the same directory as your other modules, and let them import from each other from the same directory.

I would ultimately want to be able to share python programs.

After you configure it as a separate file, you can configure it for distribution using distutils . This way, you don’t have to worry about where it should be installed - distutils will worry about you. There are many other additional distribution tools, many OS-specific - distutils work for modules, but if you want to distribute the proper program that users need to run, there are other options, such as using py2exe for Windows.

As for the module / package differences, well, here it is. If you have a whole bunch of classes you want to split so you don't have one big mess of python file, you can split it into several python files in a directory and provide a __init__.py directory.It is important to note that with Python there is no differences between a package and any other module. A package is a module, it's just another way of representing data in a file system. Similarly, a module is not just a .py file β€” if it is, sys will not be a module since it does not have a .py file. It is built into the interpreter. There are many ways to represent modules in the file system, since you can add import hooks that can create paths other than directories and .py files to represent modules. Hypothetically, you can create an import hook that used spidermonkey to load Javascript files as Python modules.

+2
source
 from [module] import [classname] 

Where the module is located somewhere in your python path.

+1
source

About modules and packages:

  • a module is a file ending with .py . You can put your class in such a file. As Andy said, he should be on your way to the python ( PYTHONPATH ). Usually you add an additional module to the same directory as your script, although it can be directly imported.

  • A package is a directory containing __init__.py (may be empty) and contains module files. Then you can import la from <package>.<module> import <class> . Again this should be in your python path.

You can find more in the documentation .

+1
source

If you want to save your RSS file in another place, use sys.append ("") and enter the module in this directory and use import or import *

0
source

The first file you created the class in is "first.py"

first.py:

 class Example: ... 

You create a second file in which you want to use the class contained in "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, simply enter:

second.py:

 from first import Example ... 
0
source

All Articles