简体   繁体   English

Distutils-我要去哪里了?

[英]Distutils - Where Am I going wrong?

I wanted to learn how to create python packages, so I visited http://docs.python.org/distutils/index.html . 我想学习如何创建python包,因此访问了http://docs.python.org/distutils/index.html

For this exercise I'm using Python 2.6.2 on Windows XP. 在本练习中,我将在Windows XP上使用Python 2.6.2。

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: 我的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)

And my setup.py file is: 我的setup.py文件是:

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/. 我运行python setup.py sdist,它创建了MANIFEST,dist /和build /。 Next I ran python setup.py install and it installed it to my site packages directory. 接下来,我运行python setup.py install并将其安装到我的站点软件包目录中。

I run the python console and can import the person module, but I cannot import the Person class. 我运行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

I checked the files added to site-packages and checked the sys.path in the console, they seem ok. 我检查了添加到站点程序包中的文件,并在控制台中检查了sys.path,它们看起来还不错。 Why can't I import the Person class. 为什么不能导入Person类。 Where did I go wrong? 我哪里做错了?

person/
   __init__.py
   person.py

You've got a package called person , and a module inside it called person.person . 您有一个名为person的程序包,并且其中有一个名为person.person的模块。 You defined the class in that module, so to access it you'd have to say: 您在该模块中定义了该类,因此要访问它,您必须说:

import person.person
p= person.person.Person('Tim', 42)

If you want to put members directly inside the package person , you'd put them in the __init__.py file. 如果要将成员直接放在打包person内部,则可以将它们放在__init__.py文件中。

Your question isn't really about distutils packages, but about Python packages -- a related but different thing with the same name. 您的问题不是关于distutils软件包,而是关于Python软件包-一个相关但不同的名称。 Packages in Python are a separate kind of module, that are directories with an __init__.py file. Python中的软件包是一种单独的模块,是带有__init__.py文件的目录。 You created a person package with a person module with a Person class. 您使用带有Person类的person模块创建了一个person包。 import person gives you the package. import person给您包裹。 If you want the person module inside the person package, you need import person.person . 如果你想在person内部模块person包,需要import person.person And if you want the Person class inside the person module inside the person package, you need from person.person import Person . 如果要在person包内的person模块中包含Person类,则需要from 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? 另请参见我应该在自己的.py文件中创建每个类吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM