Search code examples
pythonjupyter-notebookpython-importlib

Python import from "parent" folder with jupyter notebook


I have a project structure like that:

PRJ_V2
   venv
   logs
   run.py 
   MyPackage
      __init__.py
      myclass.py
      myclass2.py
   Analysis
      predictive.ipynb

in myclass.py I have a class Myclass

In run.py I can import it with from MyPackage.myclass import Myclass and run the program without problems.

but in predictive.ipynb I can't. Also as I am making changes in myclass I need to import it with importlib.import_module, to allow me to refresh the module. I've tried with all combinations without success like importlib.import_module("myclass", "MyPackage") or importlib.import_module("myclass", "..") (and "...")

With "regular" import: like from ..MyPackage.myclass import Myclass throws "attempted relative import beyond top-level package", and with from MyPackage.myclass import Myclass throws "No module named MyPackage" error

I'm a bit saturated with reading questions here without finding a solution, but I still don't understand how the system really works, and if there is another way to do it. I'm using pyhton version 3.7

The only condition here is that run.py must be able to work as it does now (it is called from a shedueled system script, doing a "cd PRJ_V2" to change directory, activate venv and execute ".\venv\Scripts\python.exe run.py"), and at the same time I need to use notebook to do manual analysis.

Thanks in advance.


Solution

  • There is many ways of solving this issue, adding the path for instance will work, but maybe the easier way is going to the proper project root directory using jupyter magic commands:

    You can check your directory running in the first cell:

    %pwd
    

    And then you can go to the parent directory using:

    %cd ..
    

    After this, if you are in the project root directory you can make your imports normally. Next time you can just run your Jupyter session from the parent directory and you won't need to navigate there.