Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

python - ModulNotFoundError even though !pip freeze tells you it is downloaded on ubuntu through WSL

I am using Ubuntu18.04 through Windows Subsystems for Linux(WSL)

on ubuntu I run jupyter lab and try to import libraries however even though !pip freeze command states that all libraries are installed outputs ModulNotFoundError

Everything works find on python script when I run main.py it imports all dependencies without a problem.

When I run jupyter lab on ubuntu with WSL it seems like it doesn't bring dependencies with it, how can I fix it?? Weird thing is !pip freeze inside jupyter lab tells me I have all dependencies installed.

Thanks in advance!

EDIT: When I install package on jupyter lab it says Requirements already satisfied . Also this only happens when I spin up jupyter lab on virtual env. When I run on global environment it seems to work fine.

EDIT2: it seems to work with jupyter notebook.... not really sure why though and would love to know the reason if anyone is knowledgeable of this matter.

question from:https://stackoverflow.com/questions/65916002/modulnotfounderror-even-though-pip-freeze-tells-you-it-is-downloaded-on-ubuntu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suspect that the pip executable might be pointing the a different version of python than what you're currently using in your notebook.

You might try running pip using the %pip magic function instead of running the inline shell command !pip, i.e.

%pip freeze

and

%pip install your-missing-package

Explanation

%pip is a line-magic function that uses the current executable (sys.executable).

In contrast, !anycommand ... runs the shell command:

sh anycommand ...

using the old-school sh shell interpreter (not bash as you might expect). Therefore, it is possible that the system executable pip that you're running (via !pip ...) uses a different version of python from sys.executable.


Alternative

Alternatively, if this doesn't work, you could use sys.executable in your shell command (in a notebook cell):

import sys

!{sys.executable} -m pip freeze

Similarly, to install packages, you might want to run:

!{sys.executable} -m pip install your-missing-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...