In lesson 25 of the raspberry pi tutorials, I encounter an error after the command “sudo pip3 install smbus2”. All steps before this command worked out fine, but here I get the error “externally managed environment” with some additional advises which I did follow but do not work either.
The more recent versions of raspian do not normally allow one to install python libraries into the main environment via pip. This has been done to protect system python libraries.
The correct way now is to create a virtual environment which allows installation outside the system libraries. Or more specifically inside a private one.
Its a fairly straightforward process. Just Google for python venv. Heres a sample site giving instructions. Start at section 5
You can also override the error using the
--break-system-packages
option flag, but this could break your system and is not recommended.
Sunfounder may have tested this override internally, for this experiment and know it to be safe. I have no idea, so I cannot recommended this route. I also understand that this override is soon to be deprecated, if not already done so.
My best guess is that the tutorial was created on an older OS where this wasn’t required. Sunfounder would be able to confirm this.
There is also a python wrapped version of smbus. This can be safely installed using apt. It should be a direct swap out for smbus, this may be an easier option for you. IMHO in general if you can find a python3 wrapped version of any pip install, its often the best option.
I.e
sudo apt install python3-smbus
Or if its smbus2
sudo apt install python3-smbus2
No venv or other complexities required.
You could also try the pip install with the –user flag instead of sudo