0

I am running into a very frustrating syntax error that is appearing in the following scenario:

package
    __init__.py
    module1.py
    module2.py

The __init__.py contains the following import:

from . import module1

module1.py contains the following import:

from . import module2

I get a syntax error right when it hits the explicit relative import in module1.py, so it would seem that the import in __init__.py is working correctly.

Earlier, I had things setup where I was importing module1.py like this:

from package import module1

Which worked correctly, so I am very confused as to why I am getting this error...

Any help would be much appreciated! Thanks!

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • Can you show us the traceback? – blhsing Oct 12 '18 at 10:25
  • Have you tried deleting any compiled files (those with file extension `*.pyc`; more info in [this answer](https://stackoverflow.com/a/2998228/9225671)) ? Sometimes they do not get renewed and contain old versions that do not match your current code. – Ralf Oct 12 '18 at 10:40

2 Answers2

0

I ended up going back to the previous working state and rewrote from there. It seems like the issue disappeared, so at least I am able to move forward, but I unfortunately don't know what caused it.

During the rewrite, I did run into a similar issue (Syntax Error) that had to do with the way I was attempting to import functions from a module within the package like so:

from . import module.function as name # causes syntax error

from .module import function as name # seems to work correctly

It seems like that might have been involved, but that was not part of the code when I initially posted this question, so I am unsure if it is relevent...

0

Why don't you just use import module1 and import module2? All 3 files are in the same directory.

Dluzak
  • 315
  • 1
  • 11
  • That should work as well, technically, since this is Python 2. However, I am trying to avoid implicit relative imports. – SnippedPaws Oct 20 '18 at 22:41