Search code examples
pythoninstallationvirtualenvironment

Installing pigpio into a virtual environment


I wanted to ask on how I can install a library appropriately in a virtual environment. I am currently having difficulties using a Servo library called pigpio upon installation when I activate a virtual environment on my Raspberry Pi Zero W.

The problem arise if I leave out ‘sudo’ in my bash commands in terminal (i.e.python myscript.py) since using the command ‘sudo python myscript.py’ would render another module of this script (Boto3) unavailable in the virtual environment.

The error message when I used the sudo command goes: Traceback (most recent call last): File "myscript.py", line 13, in import pigpio ImportError: No module named Boto3

On the other hand, using a non-sudo bash command results in pigpio, the Servo library module being unavailable.

I suspect the problem lies in the process of installation of pigpio library as I had to use ‘sudo make install’ in one of the steps. I think I might need to install via an alternative method that avoids global installation i.e. sudo. My understanding is that only pip can install packages into a virtualenv (from Pypi) (v.s. sudo which leads to global installation). Is there a way that I can find another way to appropriately install a library into a virtual environment?


Solution

  • The easiest way would be to uninstall all dependencies from the system wide modules:

    sudo pip uninstall boto3 pigpio
    

    cd to a directory like /home/your_user and then retry installing the dependencies in the venv like:

    virtualenv venv_test
    source venv_test/bin/activate
    pip install boto3
    cd pigpio
    

    Maybe a hacky edit to https://github.com/joan2937/pigpio/blob/master/Makefile#L34 to be inside your venv folder? like $(pwd)/venv_test:

    make
    make install
    deactivate
    source venv_test/bin/activate
    

    It can get complicated with permissions and module paths if using --system-site-packages or something similar.

    Also be sure that you have permissions in the directory where the venv folder is. so like if you ls -ail . you should see the non-sudo user has chown craver2000:craver2000 the_dir

    You may also be able to do something like:

    sudo cp -R /path/to/dist-packages/pigpio venv_test/lib/python2.7/site-packages
    sudo chown -R $USER venv_test/lib/python2.7/site-packages
    

    And upon further examination you might be able to just copy the pigpio file to your virtualenv like:

    deactivate
    cp pigpio/pigpio.py venv_test/lib/python2.7/site-packages
    source venv_test/bin/activate
    

    And digging a bit more here is an untested install, but hey the module imports using a venv :):

    162  sudo apt-get install build-essential
      163  git clone https://github.com/joan2937/pigpio.git
      164  pip
      165  pip install virtualenv
      166  python -m pip install virtualenv
      167  sudo pip install virtualenv
      168  sudo pip install --upgrade pip
      169  virtualenv
      170  virtualenv venv_test
      171  cd pigpio/
      172  ls
      173  nano Makefile
      174  make
      175  make install
      176  mkdir /opt/pigpio
      177  sudo mkdir /opt/pigpio
      178  chown $USER /opt/pigpio
      179  sudo chown $USER /opt/pigpio
      180  make install
      181  source ../venv_test/bin/activate
      182  ls
      183  make install
      184  sudo mkdir /usr/lib/python3.5/site-packages
      187  sudo chown $USER /usr/lib/python3.5/site-packages
      211  sudo chmod 4755 /sbin/ldconfig
      213  sudo chmod 4755 /sbin/ldconfig.real
      214  make install
      215  history