12

According to the python docs, relative importing and intrapackage referencing has been supported since python 2.5. I am currently running Python 2.7.3. So, I tried to implement this in my own package in order to use it for simpler importing. I was surprised to find it threw a SyntaxError exception at me, and I was hoping someone could help lead the way to the cause.

I setup a test directory for testing:

tester
├── __init__.py
├── first_level.py
└── sub
    ├── __init__.py
    └── second_level.py

Both __init__.py modules are empty. The other modules are:


# first_level.py
print "This is the first level of the package"

# sub/second_level.py
import ..first_level
print "This is the second level"

When I attempt to import the second_level module, I get the following error:

Python 2.7.3 (default, Aug  1 2012, 14:42:42) 
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Welcome!
>>> import tester
>>> import tester.sub.second_level
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tester/sub/second_level.py", line 1
    import ..first_level
           ^
SyntaxError: invalid syntax

I expected the two lines to print one after the other, but it raises an exception instead. So, am I doing the import wrong? Do you have any other ideas.

scicalculator
  • 1,498
  • 3
  • 16
  • 33

2 Answers2

16

You can't import modules like that. import ..blah is not valid import syntax. You need to do from .. import first_level.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • 4
    Base on the [official documenation](https://docs.python.org/2/tutorial/modules.html#intra-package-references) this IS VALID syntax in case the `blah` is a package. Check the example from the link above and namely `from ..filters import equalizer` where `filters` is a package and `equalizer` - a module. You should not generalize like that. – rbaleksandar Feb 17 '16 at 15:11
  • 4
    @rbaleksandar: No, the example uses `from`. `from ..blah import stuff` is valid. `import ..blah` is not. You can only use dot-notation relative imports with the `from` form of `import` (as mentioned in the documentation you linked to). – BrenBarn Feb 17 '16 at 19:28
  • 1
    I know that but your comment can be read as a general conclusion. You should add something like "In your case you can't import ... because ...". – rbaleksandar Feb 17 '16 at 20:01
  • 1
    I still got a "misleading" syntax error on the import line, but it turned out the syntax error was inside the file I tried to import and not on the actual import line. Just something to keep in mind :) – smerlung Jan 27 '19 at 17:29
-2

I usually do something like:

import sys 
sys.path.append("/home/me/tester")
import first_level 

hope this helps. ~Ben

francis
  • 9,525
  • 2
  • 25
  • 41
Ben
  • 669
  • 8
  • 14
  • 2
    This is a bad idea as it pollutes the global `sys.path`, leading to possible confusion later if a module in `tester` has the same name as a module elsewhere. The whole point of packages is to avoid that by creating a separate package namespace, with only the top-level package in the global `sys.path`. – BrenBarn Sep 13 '12 at 03:33