简体   繁体   English

我无法在 pycharm 中导入 pygame

[英]I can't import pygame inside pycharm

I can't import pygame inside pycharm .我无法在pycharm导入pygame
I've been using Linux Mint 19, Anaconda 3, Pycharm 2019.3.1.我一直在使用 Linux Mint 19、Anaconda 3、Pycharm 2019.3.1。
I've created a conda environment and install pygame: conda install pygame -c cogsci我已经创建了一个 conda 环境并安装了 pygame: conda install pygame -c cogsci
Then I ran (my_env) tiago@tiago-XPS-8700:~$ python3 mycode.py .然后我运行(my_env) tiago@tiago-XPS-8700:~$ python3 mycode.py And I got the following error:我收到以下错误:

teste.py:3: RuntimeWarning: use mixer: libSDL_mixer-1.2.so.0: cannot open shared object file: No such file or directory (ImportError: libSDL_mixer-1.2.so.0: cannot open shared object file: No such file or directory) pygame.mixer.music.load('bat.mp3') Traceback (most recent call last): File "teste.py", line 3, in pygame.mixer.music.load('bat.mp3') File "/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/ init .py", line 105, in getattr raise NotImplementedError(MissingPygameModule) NotImplementedError: mixer module not available (ImportError: libSDL_mixer-1.2.so.0: cannot open shared object file: No such file or directory) teste.py:3:运行时警告:使用混合器:libSDL_mixer-1.2.so.0:无法打开共享对象文件:没有那个文件或目录(导入错误:libSDL_mixer-1.2.so.0:无法打开共享对象文件:没有那个文件或目录)pygame.mixer.music.load('bat.mp3') 回溯(最近一次调用最后一次):文件“teste.py”,第 3 行,在 pygame.mixer.music.load('bat.mp3')文件“/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/初始化的.py”,线路105,在GETATTR提高NotImplementedError(MissingPygameModule)NotImplementedError:混频器模块不可用(导入错误:libSDL_mixer -1.2.so.0:无法打开共享对象文件:没有那个文件或目录)

After reading some blog at internet I installed sudo apt install libsdl-mixer1.2 .在互联网上阅读了一些博客后,我安装了sudo apt install libsdl-mixer1.2
After that if I run (my_env) tiago@tiago-XPS-8700:~$ python3 mycode.py at Linux terminal at my conda environment, my code run successfully.之后,如果我在(my_env) tiago@tiago-XPS-8700:~$ python3 mycode.py环境的 Linux 终端上运行(my_env) tiago@tiago-XPS-8700:~$ python3 mycode.py ,我的代码将成功运行。
But If I try to run the same code inside Pycharm I get the following error:但是如果我尝试在Pycharm运行相同的代码,我会收到以下错误:

/home/tiago/anaconda3/envs/pycharm/bin/python /home/tiago/Documentos/PYTHON/cursoemvideo/exercicios/ex_021.py Traceback (most recent call last): File "/home/tiago/Documentos/PYTHON/cursoemvideo/exercicios/ex_021.py", line 3, in import pygame File "/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/ init .py", line 133, in from pygame.base import * ImportError: libSDL-1.2.so.0: cannot open shared object file: No such file or directory /home/tiago/anaconda3/envs/pycharm/bin/python /home/tiago/Documentos/PYTHON/cursoemvideo/exercicios/ex_021.py 回溯(最近一次通话):文件“/home/tiago/Documentos/PYTHON/cursoemvideo /exercicios/ex_021.py”,3号线,在进口pygame的文件“/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/初始化的.py”,线路133,从pygame的.base 导入 * 导入错误:libSDL-1.2.so.0:无法打开共享对象文件:没有这样的文件或目录

Process finished with exit code 1进程以退出代码 1 结束

What am I missing?我错过了什么?

在 conda 环境中,他们预先安装了软件包和库,您可以通过导入它们轻松使用它们,但在 pycharm 中,您还需要安装该软件包,以便您可以在 pycharm 中使用它

Inspired by this questions Tensorflow and Pycharm , I ran Pychram from the Linux Terminal at my conda environment and it worked!受到Tensorflow 和 Pycharm这个问题的启发,我从 Linux 终端在我的 conda 环境中运行了 Pychram,并且成功了! If I run Pycharm from Linux Mint Menu I still can't import pygame .如果我从 Linux Mint Menu 运行 Pycharm,我仍然无法导入pygame Addicionally, I inserted the following statement at my .bashrc so that I can run pycharm from anywhere in the Linux Terminal.此外,我在.bashrc插入了以下语句,以便我可以从 Linux 终端的任何位置运行 pycharm。

# Open bashrc to edit
nano .bashrc

# Add the comand line below to the end of file
# You should adapt to your path to "pycharm.sh', mine is "/opt/pycharm-community-anaconda-2019.1.3/bin". In another computer I found it at "/var/lib/flatpak/app/com.jetbrains.PyCharm-Community/current/active/files/pycharm/bin"
export PATH=$PATH:/opt/pycharm-community-anaconda-2019.1.3/bin

# Then close the terminal and reopen it in order to the modifications in .bashrc take effect
# At terminal you run
pycharm.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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