setup.py 导入无法正常工作
我正在尝试安装我创建的一个简单的 python 库,并且认为我可能缺少一个步骤。设置运行良好(或至少运行),但当我导入时,它无法按我的预期工作。 目录结构看起来像
Foo/
setup.py
README.txt
LICENSE.txt
foo/
__init__.py
bar.py
我可以做
>>> import foo
,但如果我尝试,
>>> foo.bar
我会收到以下错误
AttributeError: 'module' object has no attribute 'bar'
相反,如果我使用
>>> from foo import bar
Here is my setup.py
from distutils.core import setup
setup(
name='Foo',
version='0.1.0',
author='ctrl-c',
author_email='[email protected]',
packages=['foo'],
license='LICENSE.txt',
description='Foo does bar.',
long_description=open('README.txt').read(),
)
则不会发生错误我想我只是错过了一些东西,但我一直在查看文档并避风港还没找到。谢谢。
I'm trying to install a simple python library I created and think I may be missing a step. The setup goes fine (or runs at least) but when I import it doesn't work as I expect.
The directory structure looks like
Foo/
setup.py
README.txt
LICENSE.txt
foo/
__init__.py
bar.py
I can do
>>> import foo
but then if I try to
>>> foo.bar
I get the following error
AttributeError: 'module' object has no attribute 'bar'
Contrarily no errors occur if I use
>>> from foo import bar
Here is my setup.py
from distutils.core import setup
setup(
name='Foo',
version='0.1.0',
author='ctrl-c',
author_email='[email protected]',
packages=['foo'],
license='LICENSE.txt',
description='Foo does bar.',
long_description=open('README.txt').read(),
)
I imagine I just missed something, but I've been looking through the docs and haven't found it yet. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 setup.py 看起来不错。您如何安装您的软件包?例如:
如果您使用的是基于 RPM 的系统,则可以使用以下方法创建可安装的 RPM:
如果您希望
bar
符号作为foo
上的属性可见默认情况下,执行以下操作:在 foo/__init__.py 中:
Your setup.py appears fine. How are you installing your package? For example:
If you're on an RPM-based system you can create an installable RPM using this:
If you want the
bar
symbol to be visible as an attribute onfoo
by default, do this:In
foo/__init__.py
:您的
foo
是一个包,并且包不会自动导入模块。你必须明确地做。这就是 Python 的工作原理。您还可以执行import foo.bar
并引用foo.bar
。Your
foo
is a package, and packages don't automatically import modules. You have to do explicitly. That's just how Python works. You can also doimport foo.bar
and referencefoo.bar
then.如果你想做这样的事情,你必须用 : 填充 foo/__init__.py ,
然后,当导入 foo 时,你将能够使用 foo.bar
否则,使用
if you want to do such a thing, you have to fill the foo/__init__.py with :
and then, when importing foo, you will be able to use foo.bar
Otherwise, use the