简体   繁体   中英

Cannot Run pygame on PyCharm

I'm running python 2.7.10 on Mac OS X El Capitan. I downloaded pygame 1.9.1 and it works fine on the command line version of Python. However, if i write a program that uses pygame library on PyCharm, it shows a error message like this.

/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/jeff/PycharmProjects/Codeup/Solution.py
Traceback (most recent call last):
  File "/Users/jeff/PycharmProjects/Codeup/Solution.py", line 1, in <module>
    import pygame
  File "/Library/Python/2.6/site-packages/pygame/__init__.py", line 95, in <module>
    from pygame.base import *
ImportError: dlopen(/Library/Python/2.6/site-packages/pygame/base.so, 2): Library not loaded: @executable_path/../Frameworks/SDL.framework/Versions/A/SDL
  Referenced from: /Library/Python/2.6/site-packages/pygame/base.so
  Reason: unsafe use of @executable_path in /Library/Python/2.6/site-packages/pygame/base.so with restricted binary

How can I solve this problem?

The problem is related to El Capitan's new System Integrity Protection (SIP). One of the consequences is that child processes have only restricted access to other resources with the goal to make the system less vulnerable. This seems to apply to pycharm that runs python as a child process, whereas you don't have this problem if you run the script from the command-line.

The dirty solution : Disable SIP . Try to avoid this.

The proper solution : maintain two separate installations of python - the system python (that comes with the OS), and another python that you use for development (dev-python). The latter you can get for example from python.org or through homebrew. Also install setuptools und pip separately and make sure that the python-binaries in /usr/local/bin link to the proper counterparts of your dev-python installation. My dev-python installation resides for example under /Library/Frameworks/Python.framework/ . Make sure that the different python ecosystems are not mixed up with each other. For example I had once the problem that the build system for some python module was able to discover my dev-python, but later linked to a sys-python lib, which led to problems when I tried to import the python module.

Hope this helped.

It seems like you don't have SDL library or have some issues with it. And also, are you sure you're using Python 2.7.10? In path I see 2.6.

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