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',
a开发者_StackOverflow中文版uthor='ctrl-c',
author_email='10minutemail@10minutemail.com',
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.
Your setup.py appears fine. How are you installing your package? For example:
% cd Foo
% python setup.py install --root /tmp/fooroot
% PYTHONPATH=/tmp/fooroot python -c 'from foo import bar; print bar'
<module 'foo.bar' from 'foo/bar.py'>
If you're on an RPM-based system you can create an installable RPM using this:
% python setup.py bdist_rpm
% sudo rpm -i dist/Foo-0.1.0-1.noarch.rpm
# now should be available to python globally
If you want the bar
symbol to be visible as an attribute on foo
by default, do this:
In foo/__init__.py
:
import 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 do import foo.bar
and reference foo.bar
then.
if you want to do such a thing, you have to fill the foo/__init__.py with :
import bar
and then, when importing foo, you will be able to use foo.bar
Otherwise, use the
import foo.bar
精彩评论