astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
26.08k stars 762 forks source link

ModuleNotFoundError: No module named 'mymodule' from jupyter notebook #8891

Closed AlexAtCCRI closed 1 day ago

AlexAtCCRI commented 5 days ago

context: i have a project called mymodule that i am using uv to manage. i would like to use mymodule's venv to test out some notebooks that make use of mymodule (but the notebooks live in a different repo).

i can import the module from within the venv, uv run --all-extras --with jupyter -- python -c 'import mymodule' works. but, i can not import the module from within a notebook (run from a jupyter server within mymodules venv)

uv run --all-extras --with jupyter jupyter-lab ~/path/to/notebooks/

then create notebook, add cell with import mymodule fails with ModuleNotFoundError

bluss commented 5 days ago

From the report, it's not clear where mymodule is located and why it is expected to be able to import it. Is it a module in the source tree, or is it installed in a virtual environment?

One thing you might keep an eye on - run import sys; print(sys.path) in the notebook and inspect the paths, and also run import os; os.getcwd(); the empty string entry in sys.path will point to the current directory.

Jupyter executes notebooks with the current directory == the notebook's directory. That could be the explanation - notebooks will find and import modules in the same folder as themselves because of the '' entry in sys.path which corresponds to the current directory.

AlexAtCCRI commented 4 days ago

thanks for the reply! i have updated the issue to give more context, and description. please let me know if i did not clarify sufficiently for your help. much appreciated.

AlexAtCCRI commented 1 day ago

some more context. mymodule is a python package. i would like to use uv's venv to build/install mymodule and run some test notebooks, which live somewhere outside of mymodule.

previously i would make a venv, install mymodule in editable mode, and then install a whole jupyter stack in the venv, and run the notebooks. i was hoping i could do this faster/ligher/more reproducably with uv venv.

please let me know if this still doesnt make sense? thanks again!

bluss commented 1 day ago

I have tried this, and I can't reproduce it, importing a library module from the current project works.

I have the following suggestion as the next thing to test, it would be to run this:

uv run --all-extras --with jupyter -- python -I -c 'import mymodule'

-I for isolated would demonstrate that mymodule is really installed in the virtual environment. If that fails, it is relevant for the issue. (When I try this command executes without problem.)

AlexAtCCRI commented 1 day ago

so here is what i got,

uv run  --all-extras --with jupyter -- python -I -c 'import mymodule'
Installed 96 packages in 61ms
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mymodule'

but after inspecting the build whl, i found the problem was me. the module was not being packaged correctly (all the submodules were at the root of the dist, so the 'real' package was not importable. ! (i was updating an existing build to pyproject.toml AND updating to uv so i confounded the two)

i had to add

[tool.setuptools]
packages = ['mymodule']

in case anyone else makes this mistake. totally my fault. your suggestions work now. thanks.