简体   繁体   中英

Can't import pygame in PyCharm

Whenever I type import pygame in PyCharm it says the module isn't installed, but I can import it in the terminal in python mode.

In your py script:

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')

I recommend do add the path of pygame to the PYTHONPATH environement variable of the shell you want to execute your script from. rather than editing the sys.path in py script directly.

PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. eg:

If you are in windows you can use the set command to extend PYTHONPATH .

in linux:

# make python look in the pygameDirPath subdirectory of your home directory for
# modules and packages 
export PYTHONPATH=${PYTHONPATH}:${HOME}/pygameDirPath

This also happens when you don't configure your virtual environment correctly.

Make sure that you select the check box Inherit Global Site Packages when you are creating the project.

继承全局站点包

The main purpose of virtual environments is to manage settings and dependencies of a particular project regardless of other Python projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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