Distutils - 我哪里出错了?
我想学习如何创建 python 包,所以我访问了 http://docs.python.org /distutils/index.html。
在本练习中,我在 Windows XP 上使用 Python 2.6.2。
我按照简单的示例创建了一个小型测试项目:
person/
setup.py
person/
__init__.py
person.py
我的 person.py 文件很简单:
class Person(object):
def __init__(self, name="", age=0):
self.name = name
self.age = age
def sound_off(self):
print "%s %d" % (self.name, self.age)
我的 setup.py 文件是:
from distutils.core import setup
setup(name='person',
version='0.1',
packages=['person'],
)
我运行了 python setup.py sdist 并创建了 MANIFEST、dist/ 和 build/。接下来,我运行 python setup.py install 并将其安装到我的站点包目录中。
我运行 python 控制台并可以导入 person 模块,但无法导入 Person 类。
>>>import person
>>>from person import Person
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name Person
我检查了添加到站点包中的文件,并检查了控制台中的 sys.path,它们看起来没问题。为什么我无法导入 Person 类。我哪里做错了?
I wanted to learn how to create python packages, so I visited http://docs.python.org/distutils/index.html.
For this exercise I'm using Python 2.6.2 on Windows XP.
I followed along with the simple example and created a small test project:
person/
setup.py
person/
__init__.py
person.py
My person.py file is simple:
class Person(object):
def __init__(self, name="", age=0):
self.name = name
self.age = age
def sound_off(self):
print "%s %d" % (self.name, self.age)
And my setup.py file is:
from distutils.core import setup
setup(name='person',
version='0.1',
packages=['person'],
)
I ran python setup.py sdist and it created MANIFEST, dist/ and build/. Next I ran python setup.py install and it installed it to my site packages directory.
I run the python console and can import the person module, but I cannot import the Person class.
>>>import person
>>>from person import Person
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name Person
I checked the files added to site-packages and checked the sys.path in the console, they seem ok. Why can't I import the Person class. Where did I go wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个名为
person
的包,其中有一个名为person.person
的模块。您在该模块中定义了该类,因此要访问它,您必须说:如果您想将成员直接放入包
person
中,则可以将它们放入__init__ 中。 py
文件。You've got a package called
person
, and a module inside it calledperson.person
. You defined the class in that module, so to access it you'd have to say:If you want to put members directly inside the package
person
, you'd put them in the__init__.py
file.您的问题实际上并不是关于 distutils 包,而是关于 Python 包——一个具有相同名称的相关但不同的东西。 Python 中的包是一种单独的模块,是带有
__init__.py
文件的目录。您创建了一个person
包,其中包含一个带有Person
类的person
模块。import person
为您提供包。如果您希望将person
模块放在person
包中,则需要import person.person
。如果您想要person
包内的person
模块中的Person
类,则需要from person.person import Person.
当您不给不同的事物赋予相同的名称时,并且当您不为了类本身而将类放在单独的模块中时,这些事情会变得更加明显。另请参阅 我应该在自己的 .py 中创建每个类吗文件?
Your question isn't really about distutils packages, but about Python packages -- a related but different thing with the same name. Packages in Python are a separate kind of module, that are directories with an
__init__.py
file. You created aperson
package with aperson
module with aPerson
class.import person
gives you the package. If you want theperson
module inside theperson
package, you needimport person.person
. And if you want thePerson
class inside theperson
module inside theperson
package, you needfrom person.person import Person
.These things get a lot more obvious when you don't give different things the same name, and also when you don't put classes in separate modules for their own sake. Also see Should I create each class in its own .py file?