I've spent hours researching this small problem now, but clearly I wasn't able to figure out how to get my packages to work the way I want them to.
Here's an extract from my project structure:
package/
__init__.py
code.py
notebooks/
testing.ipynb
With __init__.py containing:
from .code.py import *
Shouldn't I be able to import the functions from code.py within testing.ipynb as follows?
from ..package import *
The error message I'm getting is:
ImportError: attempted relative import with no known parent package
Shouldn't I be able to import the functions from code.py within testing.ipynb?
cd ..
And then import on the cell below:
from package.code import *
package directory inside the notebooks directory.Using import package with the following directory structure example:
notebooks/
package/
__init__.py
code.py
testing.ipynb
import sys
sys.path.insert(0, "..")
from package.code import *
import sys
sys.path.insert(0, "../package")
from code import *
Python sets a variable with paths on where it looks for modules and packages. The working directory where the initial script was started is one of those paths. The above code adds the package directory to the path.
Refer to this answer for an in depth look on how Python path works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With