stuff/
__init__.py
mylib.py
Foo/
__init__.py
main.py
foo/
__init__.py
script.py
script.py
wants to import mylib.py
This is just an example, but really I just want to do a relative import of a module in a parent directory. I've tried various things and get this error...
Attempted relative import beyond toplevel package
I read somewhere that the script from where the program starts shouldn't in the package, and I tried modifying the structure for that like so...
stuff/
mylib.py
foo.py // equivalent of main.py in above
foo/
__init__.py
script.py
but got same error.
How can I accomplish this? Is this even an adequate approach?
Edit: In Python 2
though as long "stuff" is not in your python PATH you got no choice than adding the path.
If you know the level of your script.py from stuff you can do for example:
I'm running Python 3.4.2 on Windows 7 and tore my hair out over this.
When running either of these:
...I would get the 'Attempted relative import beyond toplevel package' error.
For me, the solution was dropping the ".." in my [test_stock.py]. The line was: from ..stock import Stock
Changed it to: from stock import Stock
.. and it works.
Folder structure:
From the PEP it appears that you cannot use a relative import to import a file that is not packaged.
So you would need to add a
__init__.py
to stuff and change your imports to something likefrom .mylib import *
However, the PEP seems to make no allowance to keep mylib packaged up in a module. So you might be required to change how you call your library functions.
Another alternative is to move mylib into a subpackage and import it as
from .libpackage import mylib
import ..foo..stuff.mylib
should be okEDIT took off the extension
After fiddling with it a bit more, I realized how to set it up, and for the sake of specificity I won't use foo bar names. My project directory is set up as...
A line in
object_editor.py
looks like...A line in
editor.py
looks like...or alternatively
The key is that in the example I gave in the question, the "main" script was being run from within the package. Once I moved it out, created a specific package (
core
), and moved the library I wanted the editors to share (ntlib
) into that package, everything was hunky-dory.