Adding a directory to sys.path, but still getting ModuleNotFoundError

I’m trying to create a group of packages in a Jupyter notebook environment that can be archived and unarchived in the current kernel environment. I’m using venv.EnvBuilder to create an environment using symlinks to the existing python executable. After using the pip in that env’s bin to install a package and adding the site-packages directory of that env to my currently running Jupyter kernel’s python interpreter’s sys.path, I still get a ModuleNotFoundError even though the package is clearly there.

I swear I’ve done this type of thing lots of times in Python to add packages at runtime, and it has worked. I’m not sure why in this environment the module can’t be found. Any pointers?

Is this really how venvs should be created, and how packages should be installed in them, in Jupyter? Jupyter has its own way of doing this. E,g. making micro kernels, or envs and activating them. Or at least some tried and tested method, that doesn’t involve sys.path.

Perhaps pip was the global pip and installed it in the global env?

Explicit is better than implicit. Remove the ambiguity, and tell pip the --target directory where you want it to install the packages.

If perseverance is insisted on, trying to make everything’s automagick work nicely with everything else’s (possibly a fool’s errand) then:

Were symlinks set to the pip executable or script, as well as the Python executable? If not, it might install globally. The use parent/site packages flag might fix that (but could be a hack - I’ve only given this little thought)

Is it the best way? Probably not. But as is usually the case, the environments we find ourselves in don’t always allow for the best way to be used. I did get this working though. I’m not exactly sure what I was doing wrong before, but it’s working as expected now.

1 Like