简体   繁体   English

即使我安装了库,也无法导入 python 中的库

[英]Unable to import libraries in python even though i have the libraries installed

I can't import libraries in python that I have installed using pip as shown here .我无法导入我使用pip中的库,如此处所示

Even other libraries such as PySimpleGui and PyGame don't work when I try to import them.当我尝试导入它们时,甚至其他库(例如 PySimpleGui 和 PyGame)也不起作用。

I have tried uninstalling and reinstalling the libraries and I am sure they are installed on my computer.我已尝试卸载并重新安装这些库,我确信它们已安装在我的计算机上。

To sort this out, you need to establish two things:要解决这个问题,您需要确定两件事:

  • where you (ie pip ) installed the packages, and您(即pip )安装软件包的位置,以及
  • where Python is looking for them Python 在哪里找他们

You can find where pip installed a package with:您可以找到pip安装 package 的位置:

pip show PACKAGE # e.g. pip show flask

Obviously, if you install using pip3 install flask , you will need to use:显然,如果您使用pip3 install flask ,则需要使用:

pip3 show flask

Now you need to see which Python you are running and where it is looking for its packages:现在你需要查看你运行的是哪个 Python 以及它在哪里寻找它的包:

import sys

print(sys.executable) # show which Python we are running
print(sys.path)       # show where it is looking for packages

Hopefully you will see you are not installing into the Python interpreter you are using.希望您会看到您没有安装到您正在使用的 Python 解释器中。

I think you might have installed them under a different folder or space than what you need.我认为您可能已将它们安装在与您需要的不同的文件夹或空间下。 For example the code below installs the library at your current space, while simply pip3 install pygame might be somewhere else.例如,下面的代码将库安装在您当前的空间,而简单pip3 install pygame可能会在其他地方。

python3 -m pip3 install pygame

I seem to have fixed the problem, i was using a different interpreter as to which my packages were installed in as pointed out by Mohammad Jafari.我似乎已经解决了这个问题,正如 Mohammad Jafari 所指出的那样,我正在使用不同的解释器来安装我的软件包。

Thanks for all your help感谢你的帮助

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

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