2

I am starting a custom package called nate-givens-toolkit.

I want to use a module from that package in a Jupyter notebook in a different folder.

Here is my structure

- nate-givens-toolkit/
  - setup.py
  - nate_givens_toolkit/
    - cloud_io.py
    - __init__.py
- project/
  - project_nb.ipynb

I am going into nate_givens_toolkit (in the console) and running: pip3 install .

The output I get looks like everything is just fine.

Processing /home/ec2-user/nate-givens-toolkit
Building wheels for collected packages: nate-givens-tooklit
  Building wheel for nate-givens-tooklit (setup.py) ... done
  Created wheel for nate-givens-tooklit: filename=nate_givens_tooklit-0.1-py3-none-any.whl size=2009 sha256=7de8c9d2930d531603c973c7d8079b66f3d4326fb274e63087128fb7d25d9e1b
  Stored in directory: /home/ec2-user/.cache/pip/wheels/17/96/2f/0073c92cfdadbb032d855f24df4725bf190d39cd1c5bb1d233
Successfully built nate-givens-tooklit
Installing collected packages: nate-givens-tooklit
  Attempting uninstall: nate-givens-tooklit
    Found existing installation: nate-givens-tooklit 0.1
    Uninstalling nate-givens-tooklit-0.1:
      Successfully uninstalled nate-givens-tooklit-0.1
Successfully installed nate-givens-tooklit-0.1

But if I go into project_nb.ipynb and write:

from nate_givens_toolkit import cloud_io as cloud

I get: ModuleNotFoundError: No module named 'nate_givens_toolkit'

The same thing happens if I run python from the console. As long as I'm in the nate-givens-toolkit directory I can import and run it fine. But if I navigate up a level and try the import I get ModuleNotFoundError.

I don't understand why pip install seems to work, but I still get this ModuleNotFoundError. I thought the whole point of the pip install would be that I could then import from files in other directory without having to worry about relative paths and such. (I've done this exact same thing in a totally different context for work and it was fine.)

I've tried the solutions for similar issues like these:

So far, haven't found anything that works.

Everything I'm doing is inside the same conda venv.

Help?

Nathaniel Givens
  • 411
  • 1
  • 3
  • 15

1 Answers1

4

I found the answer to my question here: Import py file in another directory in Jupyter notebook. (Note: it wasn't the accepted answer on that post, it was the second answer after the accepted answer.)

The trick is that I needed to do use the -e flag when using pip install.

So instead of

pip install .

I had to run

pip install -e .

Once I ran that, it worked in Python from the console and also in Jupyter.

Nathaniel Givens
  • 411
  • 1
  • 3
  • 15