Emphasis above is mine.The Python Virtual Environment (VENV) can be daunting. It overcomes a couple of issues related to Python which was talked about in the good article that was shared but in summary:Thank you again! I've made progress!!
I have a Raspberry Pi 4B with Debian GNU/Linux 12 (bookworm) and python 3.11.2
I have my library successfully installed! I even see it in /site-packages.
However,
import adafruit_ads1x15.ads1115 as ADS
returns a ModuleNotFoundError: No module named 'adafruit_ads1x15'
I'm making 'arcade' style games with my kids. I'm pretty good at the game coding and wiring, but trying to get all the components to play and talk is not my forte.The downside I've found is:
- Isolates projects which require different versions of Python. This includes a specific version an OS may need.
- Isolates Python modules/libraries of different versions. For example, I have one project that requires a specific version of a module to work.
- Increased storage for the Python/PIP installed in the VENV
- Complexity of managing the source scripts in relation to the VENV from both a management and ease of backup. For example, I'm only interested in backing up the scripts and not the underlying Python libraries to my cloud site.
So, a mixed bag and I will not mention the additional twists if your need to use SUDO to run or install libraries. That is the problem with your SUDO PIP3 ... statements. It installed the library at the system level and not in your VENV. DON'T USE SUDO IN YOUR VENV UNLESS YOU UNDERSTAND WHAT WILL HAPPEN. Many examples on the web were developed before VENV was enforced and followed the authors own preference for managing modules.
FWIW, I was able to get your module installed. The key is that in order for it to work, you need to have the VENV ACTIVATED. If trying to run the script in a CRON/SYSTEMD job, then you need to specifically run the Python in the VENV.
I'm running on a P400 with Bullseye.
You were trying to import one of the module functions (adafruit_ads1x15.ads1115) rather than the whole module. If you want to do that the format is (note capitalization)See here for more information https://docs.circuitpython.org/projects ... n/latest/Code:
>>> from Adafruit_ADS1x15 import ADS1115 as ADS
Code:
(venv1) paul@p400PI:~/pgm/vTest $ pip listPackage Version------------- -------pip 20.3.4pkg-resources 0.0.0setuptools 44.1.1(venv1) paul@p400PI:~/pgm/vTest $ pip3 install adafruit-ads1x15Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simpleCollecting adafruit-ads1x15 Downloading https://www.piwheels.org/simple/adafruit-ads1x15/Adafruit_ADS1x15-1.0.2-py3-none-any.whl (7.0 kB)...Successfully installed Adafruit-GPIO-1.0.3 adafruit-ads1x15-1.0.2 adafruit-pureio-1.1.11 spidev-3.6(venv1) paul@p400PI:~/pgm/vTest $ pip listPackage Version---------------- -------Adafruit-ADS1x15 1.0.2Adafruit-GPIO 1.0.3Adafruit-PureIO 1.1.11pip 20.3.4pkg-resources 0.0.0setuptools 44.1.1spidev 3.6(venv1) paul@p400PI:~/pgm/vTest $ python --versionPython 3.9.2(venv1) paul@p400PI:~/pgm/vTest $ python3 --versionPython 3.9.2(venv1) paul@p400PI:~/pgm/vTest $ pythonPython 3.9.2 (default, Dec 1 2024, 12:12:57)[GCC 10.2.1 20210110] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import Adafruit-ADS1x15 as ADS File "<stdin>", line 1 import Adafruit-ADS1x15 as ADS ^SyntaxError: invalid syntax>>> import Adafruit_ADS1x15 as ADS>>> dir(ADS)['ADS1015', 'ADS1115', 'ADS1x15', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']>>> exit()(venv1) paul@p400PI:~/pgm/vTest $
1. install the venv without pip in it.
2. why backup the venv at all? All you need is a test file with the modules needed in the venv, which you can create with pip at any time you add/update/remove something from your venv with pip freeze.
pip freeze > requirements.txt
To install your dependencies:
pip install -r requirements.txt
With this you requirements.txt file becomes part of your source, you check that in and if you ever need to install your project again you get it from source and run pip install -r and you're done.
The problem with all of this is not that it's complicated or black magic, or whatever else. It is that the user is usually not interested in finding out how to use the way they need to be used first, but just want to copy and paste something from the internet and get it to work. That only works until something changes (like the OS, python version, whatever else) and then all hell breaks loose.
python + venvs are not complicated at all, it just takes a little time to read and do a couple of practice runs, like the one in the link I provided above, to see what's going on.
A python venv is nothing but a set of folders where modules get installed and when it is "activated" it just changes the current session's path where python looks for things first before looking somewhere else.
The sudo part has nothing to do with pip or python or venv works at all. It is 100% on how sudo works. And here again, people just copy and paste from the web without understanding what it really does and why, so when it comes time to use it somewhere else it becomes "virtual environment insanity" instead of "I don't know how this works".
I understand people just want to get things done, but as with anything else, a bare minimum of understanding is required if an exact set of instructions is provided that works at this point in time with the current system's configuration.
It's the way everything works: driving a car, using a spreadsheet, bee keeping, etc. You can certainly do all of those things without any prior knowledge or training too, but they also come with a lot of pain and insanity too.
Take the time to go through the link I sent. It right away shows how to create, activate and deactivate a venv. Which sounds like more magic than it is. The rest of the entire write up explains exactly what it really is, with code for the user to follow along and experiment with to actually understand what's really going on. It's just a set of folders, and a change of paths where python looks for things. That is all there is to it.
Statistics: Posted by memjr — Thu Jan 02, 2025 5:52 pm