天天看點

【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib

目錄

  • 在pycharm中安裝pygame和matploylib
    • 在pycharm導入第三方庫
    • 在pycharm中導入庫,但未用pip installxxx安裝庫

在pycharm中安裝pygame和matploylib

之前介紹了在windows系統中用pip安裝pygame和matplotlib,但如果不在pycharm中導入該庫的話,也無法在pycharm中使用該庫。這裡将介紹怎麼在pycharm導入第三方庫。

在pycharm導入第三方庫

  1. 打開pycharm,點選File,點選settings…
    【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib
  2. 找到 project:data_visualization 下的 project interpreter ,點選圖中_右上角_的加号。(這裡的data_visualization是我自己建的項目名稱,不同的項目名稱不一樣)
    【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib
  3. 搜尋pygame和matplotlib,選中後,點選_左下方_的 install package ,耐心等待即可。
    【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib

溫馨提示:有時候可能安裝不成功,多試幾次,大的庫安裝較慢,喝杯咖啡等下看。

  1. 檢驗安裝成果

    觀察已安裝包,發現所需的庫在裡面。

    【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib
    現在,我們可以寫代碼來試一下。下圖中,編輯框中沒有出現紅線提示,并且運作沒有報錯,說明pycharm導入庫成功。
    【python入門學習】在pycharm中安裝pygame和matploylib在pycharm中安裝pygame和matploylib

在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