目录
- 在pycharm中安装pygame和matploylib
-
- 在pycharm导入第三方库
- 在pycharm中导入库,但未用pip installxxx安装库
在pycharm中安装pygame和matploylib
之前介绍了在windows系统中用pip安装pygame和matplotlib,但如果不在pycharm中导入该库的话,也无法在pycharm中使用该库。这里将介绍怎么在pycharm导入第三方库。
在pycharm导入第三方库
- 打开pycharm,点击File,点击settings…
- 找到 project:data_visualization 下的 project interpreter ,点击图中_右上角_的加号。(这里的data_visualization是我自己建的项目名称,不同的项目名称不一样)
- 搜索pygame和matplotlib,选中后,点击_左下方_的 install package ,耐心等待即可。
温馨提示:有时候可能安装不成功,多试几次,大的库安装较慢,喝杯咖啡等下看。
-
检验安装成果
观察已安装包,发现所需的库在里面。
现在,我们可以写代码来试一下。下图中,编辑框中没有出现红线提示,并且运行没有报错,说明pycharm导入库成功。
在pycharm中导入库,但未用pip installxxx安装库
如果在cmd未用指令pip install xxx安装xxx库,但是在pycharm成功导入了,在cmd中使用import xxx也会报错。
例如,我在pycharm中导入了pygame_ai,但未使用指令pip install pygame_ai安装,会出现以下报错:
C:\Users\dell>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame_ai
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pygame_ai'
但如果再用pip install pygame_ai安装后,就可以在cmd使用。
C:\Users\dell>pip install pygame_ai
Collecting pygame_ai
Using cached pygame_ai-0.1.2-py3-none-any.whl (30 kB)
Requirement already satisfied: pygame<2 in e:\python\lib\site-packages (from pygame_ai) (1.9.6)
Installing collected packages: pygame-ai
Successfully installed pygame-ai-0.1.2
C:\Users\dell>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame_ai
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html