16
13

Can someone help me get Pygame working? [Workaround Found]

1d 23h ago by lemmy.world/u/ComicalMayhem in nostupidquestions

No idea where else to ask this.

I'm trying to get PyGame working on my machine. Installed using sudo apt install python3-pygame and it installs fine, but when I try to run a file that imports pygame, I get a module error telling me there's no module named pygame that exists. Nothing I've looked into online seems to help. I thought, maybe since the file is being run out of my Documents folder and pygame is installed in my root folder, that I'd import pygame directly from the root. I used sys.path.insert(1, '/lib/python3/dist-packages') to insert the folder and then imported pygame, which changed the error to from pygame.base import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]. I have no idea what's going on or how to fix this. Most threads online I see are for Windows, so I'm not sure how much the advice applies to Linux, and the ones that do specify for Linux have conflicting advice or don't make sense to me. I'm just trying to get the damn thing to work.

System Specs:

System: Kernel: 6.14.0-37-generic arch: x86_64 bits: 64 compiler: gcc v: 13.3.0 Desktop: Cinnamon v: 6.4.8 tk: GTK v: 3.24.41 wm: Muffin dm: LightDM Distro: Linux Mint 22.1 Xia base: Ubuntu 24.04 noble Machine: Type: Laptop System: HP product: HP Laptop 15-fc0xxx v: N/A serial: <superuser required> Chassis: type: 10 serial: <superuser required> Mobo: HP model: 8B2F v: 52.42 serial: <superuser required> part-nu: 9Q1E9UA#ABA UEFI: AMI v: F.10 date: 12/21/2023 Battery: ID-1: BAT0 charge: 34.3 Wh (100.0%) condition: 34.3/34.3 Wh (100.0%) volts: 12.7 min: 11.2 model: HP Primary serial: <filter> status: full CPU: Info: quad core model: AMD Ryzen 5 7520U with Radeon Graphics bits: 64 type: MT MCP arch: Zen 2 rev: 0 cache: L1: 256 KiB L2: 2 MiB L3: 4 MiB Speed (MHz): avg: 3515 high: 4003 min/max: 400/4386 boost: enabled cores: 1: 3987 2: 3988 3: 2457 4: 1831 5: 3973 6: 3902 7: 4003 8: 3985 bogomips: 44717 Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm Graphics: Device-1: AMD Mendocino vendor: Hewlett-Packard driver: amdgpu v: kernel arch: RDNA-2 pcie: speed: 16 GT/s lanes: 16 ports: active: eDP-1 empty: DP-1, DP-2, HDMI-A-1, Writeback-1 bus-ID: 03:00.0 chip-ID: 1002:1506 temp: 60.0 C Device-2: Chicony HP True Vision HD Camera driver: uvcvideo type: USB rev: 2.0 speed: 480 Mb/s lanes: 1 bus-ID: 5-1:2 chip-ID: 04f2:b78f Display: x11 server: X.Org v: 21.1.11 with: Xwayland v: 23.2.6 driver: X: loaded: amdgpu unloaded: fbdev,modesetting,vesa dri: radeonsi gpu: amdgpu display-ID: :0 screens: 1 Screen-1: 0 s-res: 1366x768 s-dpi: 96 Monitor-1: eDP-1 mapped: eDP model: BOE Display 0x0949 res: 1366x768 dpi: 101 diag: 395mm (15.5") API: EGL v: 1.5 platforms: device: 0 drv: radeonsi device: 1 drv: swrast gbm: drv: kms_swrast surfaceless: drv: radeonsi x11: drv: radeonsi inactive: wayland API: OpenGL v: 4.6 compat-v: 4.5 vendor: amd mesa v: 25.2.8-0ubuntu0.24.04.2 glx-v: 1.4 direct-render: yes renderer: AMD Radeon 610M (radeonsi raphael_mendocino LLVM 20.1.2 DRM 3.61 6.14.0-37-generic) device-ID: 1002:1506 Audio: Device-1: AMD Rembrandt Radeon High Definition Audio vendor: Hewlett-Packard driver: snd_hda_intel v: kernel pcie: speed: 16 GT/s lanes: 16 bus-ID: 03:00.1 chip-ID: 1002:1640 Device-2: AMD ACP/ACP3X/ACP6x Audio Coprocessor vendor: Hewlett-Packard driver: snd_pci_acp6x v: kernel pcie: speed: 16 GT/s lanes: 16 bus-ID: 03:00.5 chip-ID: 1022:15e2 Device-3: AMD Family 17h/19h HD Audio vendor: Hewlett-Packard driver: snd_hda_intel v: kernel pcie: speed: 16 GT/s lanes: 16 bus-ID: 03:00.6 chip-ID: 1022:15e3 API: ALSA v: k6.14.0-37-generic status: kernel-api Server-1: PipeWire v: 1.0.5 status: active with: 1: pipewire-pulse status: active 2: wireplumber status: active 3: pipewire-alsa type: plugin Network: Device-1: Realtek RTL8852BE PCIe 802.11ax Wireless Network [1T1R] vendor: Hewlett-Packard driver: rtw89_8852be v: kernel pcie: speed: 2.5 GT/s lanes: 1 port: f000 bus-ID: 02:00.0 chip-ID: 10ec:b85b IF: wlp2s0 state: up mac: <filter> Bluetooth: Device-1: Realtek Bluetooth Radio driver: btusb v: 0.8 type: USB rev: 1.0 speed: 12 Mb/s lanes: 1 bus-ID: 1-2:2 chip-ID: 0bda:b85d Report: hciconfig ID: hci0 rfk-id: 4 state: down bt-service: enabled,running rfk-block: hardware: no software: yes address: <filter> Drives: Local Storage: total: 476.94 GiB used: 400.83 GiB (84.0%) ID-1: /dev/nvme0n1 vendor: Samsung model: MZVL4512HBLU-00BH1 size: 476.94 GiB speed: 63.2 Gb/s lanes: 4 serial: <filter> temp: 40.9 C Partition: ID-1: / size: 467.89 GiB used: 400.82 GiB (85.7%) fs: ext4 dev: /dev/nvme0n1p2 ID-2: /boot/efi size: 511 MiB used: 6.1 MiB (1.2%) fs: vfat dev: /dev/nvme0n1p1 Swap: ID-1: swap-1 type: file size: 2 GiB used: 1.59 GiB (79.4%) priority: -2 file: /swapfile Sensors: System Temperatures: cpu: 69.9 C mobo: N/A gpu: amdgpu temp: 59.0 C Fan Speeds (rpm): fan-1: 4170 fan-2: 0 Repos: Packages: 3627 pm: dpkg pkgs: 3601 pm: flatpak pkgs: 26 No active apt repos in: /etc/apt/sources.list No active apt repos in: /etc/apt/sources.list.d/amdgpu-proprietary.list Active apt repos in: /etc/apt/sources.list.d/amdgpu.list 1: deb https: //repo.radeon.com/amdgpu/6.2/ubuntu noble main Active apt repos in: /etc/apt/sources.list.d/cappelikan-ppa-jammy.list 1: deb [signed-by=/etc/apt/keyrings/cappelikan-ppa-jammy.gpg] https: //ppa.launchpadcontent.net/cappelikan/ppa/ubuntu jammy main Active apt repos in: /etc/apt/sources.list.d/github-cli.list 1: deb [arch=amd64 signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https: //cli.github.com/packages stable main Active apt repos in: /etc/apt/sources.list.d/google-chrome.list 1: deb [arch=amd64] https: //dl.google.com/linux/chrome/deb/ stable main Active apt repos in: /etc/apt/sources.list.d/mullvad.list 1: deb [signed-by=/usr/share/keyrings/mullvad-keyring.asc arch=amd64] https: //repository.mullvad.net/deb/stable noble main Active apt repos in: /etc/apt/sources.list.d/official-package-repositories.list 1: deb http: //packages.linuxmint.com xia main upstream import backport 2: deb http: //archive.ubuntu.com/ubuntu noble main restricted universe multiverse 3: deb http: //archive.ubuntu.com/ubuntu noble-updates main restricted universe multiverse 4: deb http: //archive.ubuntu.com/ubuntu noble-backports main restricted universe multiverse 5: deb http: //security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse Active apt repos in: /etc/apt/sources.list.d/rocm.list 1: deb [arch=amd64] https: //repo.radeon.com/rocm/apt/6.2 noble main Active apt repos in: /etc/apt/sources.list.d/unit193-encryption-noble.list 1: deb [signed-by=/etc/apt/keyrings/unit193-encryption-noble.gpg] https: //ppa.launchpadcontent.net/unit193/encryption/ubuntu noble main Active apt repos in: /etc/apt/sources.list.d/google-chrome.sources 1: deb [arch=amd64] https: //dl.google.com/linux/chrome-stable/deb/ stable main Active apt repos in: /etc/apt/sources.list.d/megaio.sources 1: deb https: //mega.nz/linux/repo/xUbuntu_24.04/ ./ Info: Memory: total: 8 GiB note: est. available: 7.01 GiB used: 5 GiB (71.4%) Processes: 368 Power: uptime: 16h 43m wakeups: 3 Init: systemd v: 255 target: graphical (5) default: graphical Compilers: gcc: 13.3.0 alt: 11/12 Client: Unknown python3.12 client inxi: 3.3.34

Also, as I was typing my system specs, a random website called https://termbin.com/jdptopened with all my system specs on it. I don't know what this is or how it opened or why, and I'm lowkey scared but it's nifty ig. Anyone know how that opened?

Edit: Oh, also, I have an IDE and I know I can get it to run in the IDE because I had to for a project last semester, but I'm trying to avoid it and work on everything in text editor and from the terminal as a challenge to myself. Also pygame runs like shit through my IDE (Pycharm)

EDIT: Got one solution from a classmate in discord. I installed a virtual environment following this link (provided by lokalhorst below). After, I had to run source venv/bin/activate in the home folder of the project, where venv is the name of the virtual environment I created. This turned my terminal into a... something, I think a virtual environment. Anyways there's now a (venv) at the very start of the terminal entries now, and pygame works fine.

Ideally, I would still like to be able to run it without actually using a virtual environment. Again according to lokalhorst (huge thanks to you btw), running a virtual environment is best practice instead of modifying the OS python install, but I'm not sure if I want to have to run a venv every time I make a project. I'll figure that out later though, maybe at some point make an executable that doesn't require a venv or something. idk, still figuring things out.

Does this work? python3 -m pygame.examples.aliens

Also, is there a reason why you are using 'apt` instead of pip?

It does not. I get the same module missing error.

/usr/local/bin/python3: Error while finding module specification for 'pygame.examples.aliens' (ModuleNotFoundError: No module named 'pygame')

Using apt because pip throws an error when I try to install using it. error: externally-managed-environment and a bunch of more text suggesting apt installs with python3-xyz.

Never use your system Python. It is used by your OS and should not be tempered with. Use a virtual environment instead. Read this whole comment in stack overflow https://stackoverflow.com/questions/75608323/how-do-i-solve-error-externally-managed-environment-every-time-i-use-pip-3#75722775

I followed the instructions on the page but I still get the "module not found" error when running the file I made and the pygame.examples.aliens

Did you activate the environment before running your Python script using source venv/bin/activate? Replace venv with the directory of the environment.

I would use uv and install Pygame in a virtual environment. Link: https://docs.astral.sh/uv/

I never install packages with apt, it's always clunky and the packages are usually obsolete. And it's worse when you have to hack the sys.path.

PyCharm should be fine with a uv virtual environment.

I'm not entirely sure how to do a virtual environment, got any guide links or documentations to look at to get started?

Also yeah pygame works in Pycharm, but it runs like shit and I'm trying to do this project out of a text editor and terminal as a challenge for myself.

You'll want to use a virtual environment. You can use the system-installed python for this (but note that you should really try to avoid using system python for anything else). You'll want to do

python -m venv path/to/virtual/environment

Once you have one set up, you can use pip to install modules. So you'll want to find the python file inside the virtual environment. Go into the folder, and you should be able to find it in ../bin/python. This is what you'll want to use from now on. So to use pip to install things: path/to/virtual/environment/bin/python pip install pygame

Then, you can run programs through the virtual environment with the same method. ie, path/to/virtual/environment/bin/python path/to/game.py

If you don't specify the path, the command will assume you're trying to use the system-installed python

Following someone else's suggestions, I made a venv file called venv and installed pygame in it using venv/bin/pip install pygame from the directory where I have the main project in. Interestingly, it installed just fine, but when I try to do pip install pygame while in the virtual environment's bin folder, I get the externally managed environment error. My venv folder doesn't have an environment folder, it's just ~/path-to/venv, where venv is the name of the virtual environment. probably should have picked a different name tbh. Anyways I followed your suggestion making replacements where necessary to run the program. Dunno if I'm doing something wrong but it isn't working for me.

It's not just about being in the right folder. You need to specify the path. If you just do python file.py or pip install whatever

It will try to use system components, regardless of what folder you're in. You'll need to explicitly say, "use this specific venv version of python or pip." If you're in the bin folder of the venv, you'll need to use ./python file.py or ./pip install whatever

Eh... You didn't say what error message you got.

I literally did though.

from pygame.base import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]

That's not an error message, that's a line of code.