簡體   English   中英

無法在 PyCharm 中導入 pygame

[英]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 的路徑添加到要從中執行腳本的pygamePYTHONPATH環境變量中。 而不是直接在 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

當您沒有正確配置虛擬環境時,也會發生這種情況。

確保在創建項目時選中復選框Inherit Global Site Packages

繼承全局站點包

虛擬環境的主要目的是管理特定項目的設置和依賴關系,而不考慮其他 Python 項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM