Upgrading Python3 or just adding Python version3.6 / v3.7?

(Also submitted to support)

I would like to install the Chatterbot package on my GoPiGo3 robot (“Carl”) which is currently running:

  • Raspbian For Robots Stretch Linux Carl 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux

  • Python 2.7.13 and Python 3.5.3 are installed

  • All my programs run with #!/usr/bin/env Python3 as the first line

  • I do not use virtual environments

The Chatterbot package requires Python >=3.6, so I want to know how to upgrade my GoPiGo3 Python 3 environment? (without breaking anything!)

1 Like

I’m getting the idea that this is exactly the case for virtual environments.

I found this post to enable GoPiGo3 in virtual environments

Questions about using GoPiGo3 in virtual environments:

I have a library of GoPiGo3 modules at /home/pi/Carl/plib/ and
projects at /home/pi/Carl/Projects/ . Project code typically starts with:

#!/usr/bin/env python3

import sys
sys.path.append('/home/pi/Carl/plib')
import xyz   # from plib
  1. Will I need to change this template for virtual environments?

  2. Do GoPiGo3 programs running in virtual environments share the “use_mutex=True” (I2C mutex) with GoPiGo3 programs running in the system environment at the same time?

  3. Does adding virtual environments break anything I will run in a non-virtual environment? (I have two @reboot root level crontab jobs always running - a life.log logger and nohup_juicer that manages docking and battery levels.)

  4. Do I use pip or pip3 to install virtualenv

  5. read_the_docs for virtualenv suggests installing pipenv but then shows not using it in the virtualenv example - When do I need pipenv and when not?

I’m so confused about pyenv, virtualenv, “python3 -m venv”, pipenv, pip, pip3, and how to add Python version 3.6 or 3.7 without overriding the “system Pythons 2.7 and 3.5”

1 Like

Summary of what I have learned (success)


Virtual Python Environments With Specific Python Versions

Using:

python3 -m venv  
pyenv  

Virtual environments are part of the Python3 package (since v3.3)

  • To create a virtual environment for a project:
$ cd my_project_folder
  • using the default python3 version and no installed site packages
$ python3 -m venv  dir_name     (common to use venv for dir_name)    or
  • or to use default python3 version and the installed site packages
$ python3 -m venv --site-packages venv-w-site-packages
  • To use the virtual env:
$ cd my_project_folder
$ source venv/bin/activate    
or 
$ source venv-w-site-packages/bin/activate
(venv) $
or
(venv-w-site-packages) $

To save the current state of the environment:

(venv) $ pip freeze > requirements.txt  

To recreate installed packages in a new venv:

(venv) $ pip install -r requirements.txt

To stop using the virtual env:

(venv) $ deactivate

!!! Be sure to add the virtual environment folder to the .gitignore list if not named venv


pyenv - To run a Python version different than the installed “system python”

see https://yeti.co/blog/setting-up-a-raspberry-pi-with-raspbian-and-pyenv-running-python-35/

and see: https://github.com/pyenv/pyenv

  1. Setup Dependencies: WARNING Updating Your System Might Break Stuff !!! WARNING
sudo apt-get update && sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev openssl bzip2
  1. Run pyenv-installer:
cd ~
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
  1. Copy lines into ~/.bashrc (or any file executed by ~/.bashrc)
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. Run the .bashrc:
source ~/.bashrc     or     . ~/.bashrc
  1. To create a Python version for use in your environments
cd ~
pyenv install --list               to see what versions available
pyenv install 3.7.6

and in 15 minutes (Pi3) you should have the python available in ~/.pyenv/bin

  1. Familiarize with all pyenv commands:
    https://github.com/pyenv/pyenv/blob/master/COMMANDS.md

  2. See what python version are available and what is current

$ pyenv versions    - to shall all versions and * by current
or
$ pyenv version     - to show current version
  1. Designate a project local python version
cd <Project>
pyenv local 3.7.6
  1. create a virtual environment that uses that version of python
cd \<Project\>
python -m venv [--site-packages] venv
  1. Test it worked
$ source ~/.bashrc
$ source venv/bin/activate
(venv) $ python --version
(venv) $ deactivate
$ pyenv which python3     - displays path to python in use    

To designate a project should use the system version:

$ pyenv local --unset

To run a virtual env from the root crontab:

* * * * * /usr/bin/env bash -c 'YOUR_COMMAND_HERE' > /dev/null 2>&1
e.g.: 
@reboot /usr/bin/env bash -c 'cd /home/user/project && source /home/user/project/env/bin/activate && ./myprogram.py arg' > /dev/null 2>&1
4 Likes

Virtual Environments are great to use!

3 Likes