1

I have a package installed in /u/home/j/joelfred/python-dev-modules. It looks like:

/a
    __init__.py
    b.py

The source for b.py is simply:

def hello():
    print('hi yourself')

And for __init__.py:

import b

First, I make sure I'm in my home directory, and set my PYTHONPATH:

$ cd
$ export PYTHONPATH=/u/home/j/joelfred/python-dev-modules/

Then I run python3:

$ python3
Python 3.4.3 (default, Mar 18 2015, 17:28:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/u/home/j/joelfred/python-dev-modules/a/__init__.py", line 1, in <module>
    import b
ImportError: No module named 'b'

Okay, that's weird. But if I change __init__.py to be blank:

$ python3
Python 3.4.3 (default, Mar 18 2015, 17:28:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a.b as b
>>> b.hello()
hi yourself
>>> 

What on earth is going on?

Joel
  • 2,065
  • 2
  • 19
  • 30
  • Doesn't work, invalid syntax: Python 3.4.3 (default, Mar 18 2015, 17:28:34) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import a Traceback (most recent call last): File "", line 1, in File "/u/home/j/joelfred/python-dev-modules/a/__init__.py", line 1 import .b ^ SyntaxError: invalid syntax >>> – Joel Mar 20 '15 at 05:47
  • you should `from .b import *` – Ozgur Vatansever Mar 20 '15 at 05:47
  • Okay, that works, but why? What if I don't want `hello` in `a`'s namespace? Why doesn't the way I did it work? – Joel Mar 20 '15 at 05:49
  • If you don't want `hello` in `a`'s namespace, you should define a list with a special name `__all__` in `b` and not put `hello` in it. You might want to take a look at answers [here](http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python). – Ozgur Vatansever Mar 20 '15 at 05:51

1 Answers1

4

In Python 3 all imports are absolute. You can't do import b unless b itself is a top-level module/package available on sys.path. If you want to import b from inside a, use an explicit relative import:

from . import b
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thanks, that was driving me crazy. I'll accept your answer as soon as the system lets me. Is there someplace in the python documentation that points this out? – Joel Mar 20 '15 at 05:56
  • @Joel there may be a more friendly page that describes the Python 3 import differences, but [PEP 328](https://www.python.org/dev/peps/pep-0328/) is where the changes originated. – jedwards Mar 20 '15 at 06:01
  • @Joel: [The documentation on `import`](https://docs.python.org/3/reference/simple_stmts.html#import) gives an outline, with a link to the (lengthy) [full description](https://docs.python.org/3/reference/import.html#importsystem) of the import system. Absolute/relative imports are described in [PEP 328](https://www.python.org/dev/peps/pep-0328/). The fact that implicit relative imports used to work in Python 2 but don't in Python 3 is mentioned in basically every list of differences between Python 2 and 3, for instance [here](https://docs.python.org/3.0/whatsnew/3.0.html#removed-syntax). – BrenBarn Mar 20 '15 at 06:05
  • @BrenBarn Thanks, I feel a bit silly for missing a basic change in the language. I wish there'd been that warning on the modules documentation page somewhere. – Joel Mar 20 '15 at 06:07