Like nearly everyone that programed their GoPiGo3 in Python, I always took the easy way of setting up my robot - installing as a system-wide package (not as a virtual environment).
“Real Python” programmers have always known that putting your project in a virtual environment gave you total control and isolated your project from unintended changes, but us “hacks” have always known virtual environments add complexity we didn’t need at the time.
When I started writing GoPiGo3 scripts, I learned from the Dexter Industries GoPiGo3 Examples that if I put:
#!/usr/bin/env python <--- python2 in those days
at the top of my script and set the file as executable with chmod +x myscript.py, that I didn’t have to type out:
python myscript.py
I could just type:
./myscript.py
and it would run just fine.
Later, the gods of Python (that had given us 10 years of warnings) decided we should all stop using Python2 and only use Python 3.
So I had to go through every script I had written and change it to:
#!/usr/bin/env python3
For some reason, I decided to shorten this to:
#!/usr/bin/python3
which appeared to work just as well on all my robots with the new Python3 interpreters.
That was all good, until ModRobotics answered our requests for a new install to the latest PiOS releases that come out every year or so. ModRobotics did a true “professional job” creating the new install options, and installing into a GoPiGo3 virtual environment.
As I started moving Dave onto the new install/virtual environment, I discovered my script worked if I launched it:
python3 myscript.py
but if I launched it as I always have:
./myscript.py
the system complained “no module easysensors”.
That is weird right? I checked - yes the GoPiGo3 virtual environment is activated so the easysensors module has to exist. Indeed it is where it should be:
(gopigo3) tovli@mrgpg3:~/MRGPG3 $ find ~/ easysensors.py | grep easysensors.py
/home/tovli/KiltedDave/systests/us/easysensors.py
/home/tovli/.venv/gopigo3/lib/python3.13/site-packages/gopigo3/easysensors.py
/home/tovli/.venv/gopigo3/lib/python3.13/site-packages/easysensors.py
If I typed:
python3
>>> import easysensors
>>>
no problem. So what is going on?
Let’s ask Claude… bang right to it: "Put #!/usr/bin/env python3 at the top of your program.
Ok, let’s look at my program:
#!/usr/bin/python3
Now were at the heart of the matter - That way uses the system Python3 interpreter which uses the system path (which does not the GoPiGo3 site-packages installed. (Used to, but not anymore.)
#!/usr/bin/env python3
uses the virtual environment path to find the virtual environment Python3 interpreter and the virtual environment site packages that indeed do have easysensors and the entire GoPiGo3 Python3 API modules installed.
Argh! If I was a Linux guru, I would know how to recursively grep for #!/usr/bin/python3 and substitute #!/usr/bin/env python3 in those files.
Well I’m no Linux guru, and I never trust those kinds of “you find it and change it for me” commands anyway. So I’m finding the files manually, and editing them one by one.
Should have followed the rules…