简体   繁体   English

无法导入 Python 模块,因为“sys.path”和“sys.executable”存在差异

[英]Can't import Python module because Difference in “sys.path” and “sys.executable”

I'm install "Jupyter notebook" and also have "Jupyter notebook in Visual Studio Code" When I run "pip3 install some_module" then it can import in "Jupyter notebook in Visual Studio Code" BUT CAN'T in "Jupyter notebook" I relize that in "Jupyter notebook in Visual Studio Code" sys.executable = /usr/bin/python3 But in "Jupyter notebook" sys.executable = /home/binh/.local/my_project_env/bin/python3我正在安装“Jupyter notebook”并且还有“Visual Studio Code 中的 Jupyter notebook”当我运行“pip3 install some_module”时,它可以导入“Visual Studio Code 中的 Jupyter notebook”但不能在“Jupyter notebook”中导入在“Visual Studio Code 中的 Jupyter notebook” sys.executable = /usr/bin/python3 但在“Jupyter notebook” sys.executable = /home/binh/.local/my_project_env/bin/python3

When working with Jupyter notebooks , it makes sense having Python virtual environments containing just the modules that you need.使用Jupyter notebooks时,让 Python 虚拟环境只包含您需要的模块是有意义的。 This provides a guarantee that the notebook running on the particular versions of Python modules you have installed, will always run correctly if those modules in those versions are present.这保证了在您已安装的 Python 模块的特定版本上运行的笔记本电脑,如果这些版本中的模块存在,则将始终正确运行。

From the sys.executable paths you provided, it seems like you have multiple versions of Python installed.从您提供的sys.executable路径来看,您似乎安装了多个版本的 Python。 When you run "pip3 install <some_module>" , you are only installing that module to the system-wide Python installation , unless you are within a Python virtual environment.当您运行"pip3 install <some_module>"时,您只是将该模块安装到系统范围的 Python 安装中,除非您在 Python 虚拟环境中。 With that said, there are 2 possible solutions to your problem:话虽如此,您的问题有两种可能的解决方案:

  1. The most recommended would be to create a virtual environment as explained in detail here .最推荐的方法是创建一个虚拟环境,如此处详细说明

  2. Alternatively, you could also set the Jupyter Notebook's sys.executable path to point to the system-wide Python installation by simply changing the path to your local /usr/bin/python3 .或者,您也可以将 Jupyter Notebook 的 sys.executable 路径设置为指向系统范围的 Python 安装,只需将路径更改为本地/usr/bin/python3即可。 However, this is not recommended.但是,不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM