'My Python library will not load and adding path to .zprofile did not work
I've had python 3 insalled on my system for a few months and have never had an issue importing libraries before. After using pip3 to install pygame, IDLE couldn't find the pygame library:
Traceback (most recent call last):
File "/Users/williamh/unit-6-project-music-pyer/code/player pygame rewrite.py", line 3, in <module>
import pygame.mixer as mixer
ModuleNotFoundError: No module named 'pygame'
I have tested a sample program from command line, and pygame seems to be working and installed. Both IDLE and Visual Studio Code give modulenotfound errors. I have tried updating .zprofile with the path pygame was installed under:
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
PATH="/opt/homebrew/lib/python3.9/site-packages:$PATH"
export PATH
eval "$(/opt/homebrew/bin/brew shellenv)"
The only new lines added to .zprofile are the 2nd "PATH" command and the 2nd "export PATH" After restarting my mac, it still wont load. Here's my system specs:
MacBook Pro (13-inch, M1, 2020)
Chip: Apple M1
Memory: 8 GB
Terminal Default Shell: -zsh
Solution 1:[1]
I have found a simple solution. M1 macs default to -zsh shell for terminal. This was the problem that caused pygame to install to the wrong place. Running this code in terminal switches your default shell to -bash:
chsh -s /bin/bash
Then after that, I installed pygame again.
pip3 install pygame
Now pygame imports and works perfectly.
Solution 2:[2]
This is a common problem, it means you don't have Pygame library installed, first open your command prompt (for windows) or terminal (for Linux & mac). then you need to check for python installation :
python --version
then you go ahead and check for pip installation:
pip --version
if everything seems right you can install Pygame:
pip install pygame
if your system recognize python, pip and pygame is already installed, you should change your python interpreter in your IDE.
if you are using VScode you can change it from the bottom right corner of the app, and when you click on it "Select Interpreter" will appear then you can go ahead and select the environment you want to use.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wh_nbps |
| Solution 2 |
