Posts: 1,301
Threads: 151
Joined: Jul 2017
Jun-05-2026, 12:12 AM
(This post was last modified: Jun-05-2026, 12:12 AM by Pedroski55.)
I have never used pygame, but I want to try out a little game I am making, so I installed pygame in my VENV called GPE.
Quote:(GPE) peterr@peterr-Modern-15-B7M:~/PVE$ pip install pygame
Requirement already satisfied: pygame in ./GPE/lib/python3.12/site-packages (2.6.1)
(GPE) peterr@peterr-Modern-15-B7M:~/PVE$
Try to run the game, says no module pygame:
Quote:(GPE) peterr@peterr-Modern-15-B7M:~/PVE$ games/versuch1.py
Traceback (most recent call last):
File "/home/peterr/PVE/games/versuch1.py", line 3, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
(GPE) peterr@peterr-Modern-15-B7M:~/PVE$
My VENV GPE is active
I think maybe my shebang is wrong? I never installed pygame or anything else in the system Python, only in my VENV, GPE.
This shebang at the top of versuch1.py does not work. pygame is only installed in my VENV. (I made my little game executable, so it should run in bash.)
Quote:#! /usr/bin/python3
I tried pointing to this symlink in the VENV as the shebang:
Quote:#! /home/peterr/PVE/GPE/python3.12/bin/python3
And tried this:
Quote:#! GPE/python3.12/bin/python3
Any tips please? Is the shebang the problem??
If I start Idle, I can import pygame, no problem.
import pygame
pygame 2.6.1 (SDL 2.28.4, Python 3.12.3)
Hello from the pygame community. https://www.pygame.org/contribute.html pygame is installed in:
Quote:GPE/lib/python3.12/site-packages
I have not had this trouble with other packages, as far as I remember.
Posts: 4,904
Threads: 79
Joined: Jan 2018
Jun-05-2026, 09:16 AM
(This post was last modified: Jun-05-2026, 09:16 AM by Gribouillis.)
(Jun-05-2026, 12:12 AM)Pedroski55 Wrote: Is the shebang the problem??
Yes, the shebang is the problem. Launch the program with the python command (in the activated virtual environment)
Output: $ python games/versuch1.py
« We can solve any problem by introducing an extra level of indirection »
Posts: 7,431
Threads: 125
Joined: Sep 2016
Jun-05-2026, 04:48 PM
(This post was last modified: Jun-05-2026, 04:49 PM by snippsat.)
Some point and as mention always use python in environment so don't call stuff outside.
# Make and check that it use path in environment
om_gil_arsen@tom-down:~$ python -m venv foo_env
om_gil_arsen@tom-down:~$ cd foo_env/
om_gil_arsen@tom-down:~/foo_env$ source bin/activate
(foo_env) om_gil_arsen@tom-down:~/foo_env$ which python
/home/om_gil_arsen/foo_env/bin/python
(foo_env) om_gil_arsen@tom-down:~/foo_env$ python -c "import sys; print(sys.executable)"
/home/om_gil_arsen/foo_env/bin/python
# Install and test pygame in environment
(foo_env) om_gil_arsen@tom-down:~/foo_env$ pip install pygame
Collecting pygame
Downloading pygame-2.6.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)
Downloading pygame-2.6.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.0/14.0 MB 10.1 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.6.1
(foo_env) om_gil_arsen@tom-down:~/foo_env$ python
Python 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.6.1 (SDL 2.28.4, Python 3.12.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
Posts: 1,301
Threads: 151
Joined: Jul 2017
Thanks very much! Worked first time!
Quote:python games/versuch1.py
This is how I start the VENV in bash:
cd PVE && source GPE/bin/activate && python3 -m idlelib.idle I was surprised to find that the VENV does not carry its own interpreter!
In my VENV GPE/python3.12/bin there are 3 symlinks: python, python3 and python3.12. The first 2 point to the 3rd symlink python3.12, and that points to the binary: /usr/bin/python3.12
In /usr/bin there is a symlink python3, which points to the binary /usr/bin/python3.12
So I am a little confused that the shebangs
#! /usr/bin/python3 and #! /usr/bin/python3.12
do not work!
Isn't that exactly what the bash command: python games/versuch1.py calls??
Thanks for your help!
Posts: 232
Threads: 0
Joined: Jun 2019
Hi,
I think there's still a bit of a lack of understanding what a venv does. If you read through the first couple of paragraphs on https://docs.python.org/3/library/venv.html, it should become more clear.
A venv primarily isolates the installed packages from the main / system-wide installation. Thus allows to install e.g. different versions of a package or packages conflicting the system's packages without breaking the system. This is particular useful in Linux, as most Linux distro use Python and some Python packages as an integral part of the installation and system. To ensure the venv's modules are use, always call the Python from within the venv, so either with an activated venv calling python name_of_script.py (or python3, depending on the system) or by calling Python with its full path, e.g. /home/peterr/PVE/GPE/python3.12/bin/python3 name_of_script.py. The latter is the way to go when e.g. starting on Linux a Python script within a venv with a systemd service unit.
Regards, noisefloor
Posts: 1,301
Threads: 151
Joined: Jul 2017
Thanks for your reply! Still confused!
Interestingly, I immediately find in your link, please note the bold type:
Quote:Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project (library or application). These are by default isolated from software in other virtual environments and Python interpreters and libraries installed in the operating system.
But, as far as I can tell, the i nterpreter used resides in /usr/bin, not in the VENV.
Like I mentioned above:
In my VENV GPE/python3.12/bin there are 3 symlinks: python, python3 and python3.12. The first 2 point to the 3rd symlink python3.12, and that points to the binary:
Quote:/usr/bin/python3.12
The same 3 symlinks appear in:
Quote:GPE/lib/python3.12/site-packages/bin
and the same is true of the symlinks: python, python3 and python3.12.
The first 2 point to the 3rd symlink python3.12, and that points to the binary:
Quote:/usr/bin/python3.12
Does your VENV have an interpreter inside the VENV?
as far as I can tell, the interpreter used resides in /usr/bin, not in the VENV.
Posts: 7,431
Threads: 125
Joined: Sep 2016
(Jun-07-2026, 10:27 AM)Pedroski55 Wrote: Does your VENV have an interpreter inside the VENV? That's the whole point of victual environment that is should be isolated from OS system Python.
# Before active a venv it use OS system python
tom-down:~/foo_env$ python -c "import sys; print(sys.executable)"
/usr/bin/python
# Activate
tom-down:~/foo_env$ source bin/activate
# Then of coursed use interpreter inside venv
(foo_env) om_gil_arsen@tom-down:~/foo_env$ python -c "import sys; print(sys.executable)"
/home/foo_env/bin/python
Posts: 1,301
Threads: 151
Joined: Jul 2017
Jun-08-2026, 07:06 AM
(This post was last modified: Jun-08-2026, 07:07 AM by Pedroski55.)
Thanks, but I am still a little confused.
Quote:That's the whole point of victual environment that is should be isolated from OS system Python.
Well something is wrong with my VENV then! I assumed the interpreter binary would lie in a folder called bin.
Quote:(GPE) peterr@peterr-Modern-15-B7M:~/PVE$ python -c "import sys; print(sys.executable)"
/home/peterr/PVE/GPE/bin/python
(GPE) peterr@peterr-Modern-15-B7M:~/PVE$
This:
Quote:/home/peterr/PVE/GPE/bin/python
is a symlink. It points to Quote:python3
, another symlink in GPE/bin.
The symlink Quote:python3
points to Quote:/usr/bin/python3
There is no Python interpreter in my VENV, which is called GPE.
The activate script is also in GPE/bin.
Do you have an actual Python interpreter in your VENV foo/bin??
Posts: 1,301
Threads: 151
Joined: Jul 2017
Just to confirm the status of my folder GPE/bin I used this command in bash:
Quote:ls -al GPE/bin
In part it shows:
Quote:lrwxrwxrwx 1 peterr peterr 7 Jun 2 2025 python -> python3
lrwxrwxrwx 1 peterr peterr 16 Jun 2 2025 python3 -> /usr/bin/python3
lrwxrwxrwx 1 peterr peterr 7 Jun 2 2025 python3.12 -> python3
Posts: 7,431
Threads: 125
Joined: Sep 2016
Jun-08-2026, 01:56 PM
(This post was last modified: Jun-08-2026, 01:57 PM by snippsat.)
(Jun-08-2026, 07:06 AM)Pedroski55 Wrote: Well something is wrong with my VENV then! I assumed the interpreter binary would lie in a folder called bin. Yes,it look ok.
Do you not have python,python3 in bin folder?
If i look folder in my venv bin folder.
tom-down:~/foo_env/bin$ ls
Activate.ps1 activate activate.csh activate.fish pip pip3 pip3.12 python python3 python3.1 This is when i make environment with build in venv
python -m venv foo_env
cd foo_env/
source bin/activate
|