Python 中的外部类

发布于 2024-07-14 11:59:13 字数 75 浏览 10 评论 0原文

我刚刚开始使用 Python,我想使用外部 RSS 类。 我将该类放在哪里以及如何导入它? 我希望最终能够共享 python 程序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失眠症患者 2024-07-21 11:59:13

关于import语句:

(一篇很好的文章位于http://effbot.org/zone/import-confusion.htm 以及 Python 教程的详细信息位于 http://docs.python.org/tutorial/modules.html )

有两种将代码导入 python 程序的常规方法。

  1. 模块

模块简单是一个以 .py 结尾的文件。 对于 python,它必须存在于搜索路径中(如 sys.path 中所定义)。 搜索路径通常由正在运行的 .py 的同一目录以及 python 系统目录组成。

给定以下目录结构:

myprogram/main.py
myprogram/rss.py

从 main.py 中,您可以通过运行“导入”rss 类:

import rss
rss.rss_class()

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

包提供了一种更结构化的方式来包含更大的 python 程序。 它们只是一个包含 __init__.py 以及其他 python 文件的目录。

只要包目录在sys.path上,那么就可以像上面一样使用。


要查找当前路径,请运行以下命令:

import sys
print(sys.path)

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)
旧时浪漫 2024-07-21 11:59:13

关于模块和包:

  • 模块是以.py结尾的文件。 您可以将您的课程放入这样的文件中。 正如 Andy 所说,它需要位于您的 python 路径 (PYTHONPATH) 中。 通常,您会将附加模块放在与脚本相同的目录中,但可以直接导入。

  • 包是一个包含__init__.py(可以为空)并包含模块文件的目录。 然后,您可以从.导入 la 。 导入<类>。 同样,这需要位于您的 python 路径上。

您可以在文档中找到更多信息。

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.

终止放荡 2024-07-21 11:59:13

我不太喜欢这么晚回答,但我对现有的答案并不完全满意。

我刚刚开始使用 Python,我想使用外部 RSS 类。 我将该类放在哪里以及如何导入它?

您将其放入 python 文件中,并为该 python 文件提供扩展名 .py 。 然后您可以导入代表该文件的模块并访问该类。 假设您想导入它,则必须将 python 文件放在导入搜索路径中的某个位置 - 您可以在运行时使用 sys.path 看到这一点,并且可能需要了解的最重要的事情是站点包(特定于安装)和当前目录('')通常位于导入搜索路径中。 当您有一个同构项目时,通常将其与其他模块放在同一目录中,并让它们从同一目录相互导入。

我希望最终能够共享 python 程序。

将其设置为独立文件后,您可以使用 distutils 将其设置为分发。 这样您就不必担心它应该安装在哪里——distutils 会为您担心。 还有许多其他附加的分发方式,许多特定于操作系统的 distutils 适用于模块,但如果您想分发用户要运行的正确程序,则存在其他选项,例如使用 py2exe 适用于 Windows。

至于模块/包的区别,好吧,就这样了。 如果你有一大堆类想要划分,这样你就不会出现一个大乱七八糟的 python 文件,你可以将它分成一个目录中的多个 python 文件,并给该目录一个 __init__.py 。 需要注意的重要一点是,在 Python 中,包和任何其他模块之间没有区别。 包是一个模块,它只是在文件系统上表示模块的一种不同方式。 同样,模块不仅仅是 一个 .py 文件——如果是这样的话,sys 将不是一个模块,因为它没有 .py 文件。 它内置于解释器中。 在文件系统上表示模块的方法有无数种,因为您可以添加导入挂钩,这些挂钩可以创建除目录和 .py 文件之外的其他方式来表示模块。 假设,我们可以创建一个导入钩子,使用 Spidermonkey 将 Javascript 文件加载为 Python 模块。

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.

雨轻弹 2024-07-21 11:59:13
from [module] import [classname]

该模块位于 python 路径上的某个位置。

from [module] import [classname]

Where the module is somewhere on your python path.

巴黎夜雨 2024-07-21 11:59:13

如果您想将 RSS 文件存储在不同的位置,请使用 sys.append("") 并将模块放在该目录中并使用
导入或从导入 *

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 *

成熟的代价 2024-07-21 11:59:13

在其中创建了类的第一个文件是“first.py”

class Example:
   ...

myprogram/first.py
myprogram/second.py

然后在第二个文件中,要调用第一个文件中包含的类,只需键入:

second.py:

from first import Example
...

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
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文