[SOLVED] Install brickpi3 in a virtual environment


Ok, this one has been apparently answered before but not to my satisfaction and I am guessing not to that of others.

First answer : don’t use virtual environments (VEs) for python on the pi. I think this is just wrong. VEs are standard practice. Wouldn’t it be better to modify, if needed, the brickpi install to handle VEs with perhaps a pip install ?

Second answer from RobertLucian:

I cd-ed to ~/Desktop where the user has rw permissions.

Created a virtual environment with the virtualenv env --python=python3 command.

Activated the env environment with source env/bin/activate.

Created another folder on the Desktop called env-test, where than I cd-ed to it.

git clone https://github.com/DexterInd/BrickPi3.git - we need the user to have rw access to it, otherwise, pip freeze won’t show up our package.

cd-ed to ~/Desktop/env-test/BrickPi3/Software/Python.

Run the following commands in this order:

python setup.py clean - removes the old build (if there’s one)

python setup.py build - creates a new build

python setup.py install - installs the current build

Install spidev package with pip install spidev.

Type python and try to import the brickpi3 module - it’s going to work.

How is this is supposed to work?

setup.py creates this file : /usr/local/lib/python3.5/dist-packages/brickpi3-0.0.0-py3.5.egg/,

which is some kind of compressed file that contains brickpi3.py? Outside the virtual environment, brickpi3 will import and its location (given by “brickpi3.file” ) is /usr/local/lib/python3.5/dist-packages/brickpi3-0.0.0-py3.5.egg/brickpi3.py. How can that be given the .egg file is not a directory?

When you run setup.py from within the virtual environment, it will create the .egg file in the same location.
In other words, the module is inaccessible from the VE. If, as in my case, your VE is called cv, doesn’t brickpi3.py (and other files?) need to end up ~/.virtualenvs/cv/lib/python3.5?

Could someone show in detail how to get around this problem? I just spent half a day installing OpenCV on my pi in a VE where it works very nicely. Really hate to have to do it again.



The answer seems to be : you need to be both in the VE and run setup.py from within the VE directory. So in my case, I put the BrickPi3/Software/Python folders and files in ~/.virtualenvs/cv and ran setup.py from there. That way, the .egg file ends up in site-packages folder cv/lib/Python3.5. Appears to work!