[英]Can't import pygame in PyCharm
每當我在 PyCharm 中鍵入import pygame
時,它都會說該模塊未安裝,但我可以在 python 模式下將其導入終端。
在您的 py 腳本中:
import sys
##lets check if this module appears in the sys.path
exist = False
for path in sys.path:
if 'pygame' in path:
print path
exist = True
if not exist:
sys.path.append('... the path to the pygame dir.. where the __init__.py')
我建議將 pygame 的路徑添加到要從中執行腳本的pygame
的PYTHONPATH
環境變量中。 而不是直接在 py 腳本中編輯sys.path
。
PYTHONPATH
是一個環境變量,您可以設置它以添加其他目錄,python 將在其中查找模塊和包。 例如:
如果您在windows
中,則可以使用set
命令擴展PYTHONPATH
。
在 linux 中:
# make python look in the pygameDirPath subdirectory of your home directory for
# modules and packages
export PYTHONPATH=${PYTHONPATH}:${HOME}/pygameDirPath
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.