jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 6:11 pm

I have a raspberry pi 3B and just installed the latest Raspian Jessie. The current version installed with the latest distribution is Python 3.5.3.

I'm using virtual environments for each project. Before I get started setting this thing up should I upgrade to Python 3.7.1 (or some other version) or stick which the current version that came latest Raspian dist?

Appreciate the help - Jerry

DirkS
Posts: 10018
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 7:08 pm

If you don't *need* to get a more recent version than I would advise against changing it.
It's a bit more involved than just downloading + installing.

gordon77
Posts: 4312
Joined: Sun Aug 05, 2012 3:12 pm

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 7:18 pm

You could try this, changing version numbers as appropriate..

https://gist.github.com/dschep/24aa6167 ... 824400d37f

Andyroo

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 8:39 pm

If you take a break 3.8 will come out :lol:

From me its +1 for
DirkS wrote:
Mon Dec 10, 2018 7:08 pm
If you don't *need* to get a more recent version than I would advise against changing it.
It's a bit more involved than just downloading + installing.
Upgrades break things more often than fixing bits (says he who has bled from the bleeding edge before).

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 9:49 pm

Thanks for the replies. I'm in no hurry to upgrade python. I thought the time was good since making changes and refactoring code. I'm currently in a struggle just going from 2.7 to 3.5. Can't find smbus on 3.5 as of yet. If anyone knows what the python 3.5 solution for "import smbus" is I would be grateful for that info.

Jerry

hippy
Posts: 6297
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 10:03 pm

Some time ago following these instructions for installing version 3.6.0 worked for me -

https://bohdan-danishevsky.blogspot.com ... -pi-3.html

Code: Select all

cd ~
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
tar -zxvf Python-3.6.0.tgz
cd Python-3.6.0
./configure
You can probably change the version with the process remaining the same. I have no idea if that makes it the default or not. And I'm not going to recommend anything when it comes to 'pip'.

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 11:17 pm

jerryk wrote:
Mon Dec 10, 2018 9:49 pm
Can't find smbus on 3.5 as of yet. If anyone knows what the python 3.5 solution for "import smbus" is I would be grateful for that info.

Code: Select all

$ apt-cache search smbus | grep -i python
python-smbus - Python bindings for Linux SMBus access through i2c-dev
python3-smbus - Python 3 bindings for Linux SMBus access through i2c-dev
so …

Code: Select all

sudo apt install python3-smbus
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Mon Dec 10, 2018 11:46 pm

Code: Select all

sudo apt install python3-smbus
I did try that before I get the following output:
python3-smbus is already the newest version (3.1.2-3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded
and when I try to import smbus I get a module not found error

I did import smbus2 and that seems to be working so far. The next issue that no temp sensors work now and all read 365.95 degrees. I'll keep looking into that issue.


Jerry

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Upgrade Python from 3.5.3 to latest version

Tue Dec 11, 2018 6:28 pm

and when I try to import smbus I get a module not found error
It seems you have installed python3-smbus globally and do not have access to it inside your virtual environment.
Try setting up your environment using the system-site-packages flag:

Code: Select all

python3 -m venv <your_env_name>  --system-site-packages

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Tue Dec 11, 2018 8:31 pm

I am using a virtual environment that was created using:

Code: Select all

python3 -m venv venv/
when I try to import smbus using the following two differing methods this output is observed:
(venv) [email protected]:~/projects/venv $ pip3 install smbus
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting smbus
Could not find a version that satisfies the requirement smbus (from versions: )
No matching distribution found for smbus

(venv) [email protected]:~/projects/venv $ sudo apt-get install python3-smbus
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-smbus is already the newest version (3.1.2-3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

In addidtion to the smbus issue another problem is with spidev.

It imports fine and seems to run in the code however this code returns all zeros:

Code: Select all

import spidev

spi = spidev.SpiDev()
spi.open(0, 0)

def readadc(adcnum):
    print(adcnum)
    # read SPI data from the MCP3008, 8 channels in total
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    print(r)
    data = ((r[1] & 3) << 8) + r[2]
    print(data)
    return data

result = readadc(3)
print(result)
So the general question about spidev is does it work with python 3 and the mcp3008 chip? I have verified the power pins and signal inputs to make sure of no obvious hardware snafus. "lsmod" returns:
spi_bcm2835 16384 0
This is a fairly mature project that has worked in my home security system for over a year now and the upgrade from 2.7 to 3.5.3 has been problematic to say the least.

Also noteworthy is that I did not just try to upgrade the python itself, instead I started with a fresh noobs install on a new card and switched cards in the pi that controls the security system interface.


If it is more appropriate to start another topic surrounding spidev on python 3 let me know.

Jerry

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Upgrade Python from 3.5.3 to latest version

Tue Dec 11, 2018 9:18 pm

I don't think your code

Code: Select all

(venv) [email protected]:~/projects/venv $ sudo apt-get install python3-smbus
is acting as you want.
You can run apt-get from anywhere (including from a python virtual environment) and it will still perform a global install.
Have you tried the system-site-packages flag?

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Wed Dec 12, 2018 12:33 am

I set up a new venv called venv1 with the following:
python3 -m venv venv1/ --system-site-packages
The resulting pip freeze within the activated environment does not show smbus or any variant of it.

Trying:
sudo apt install python3-smbus
Returns:
python3-smbus is already the newest version (3.1.2-3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Trying:
sudo find / -name python3-smbus
Returns:
/usr/share/doc/python3-smbus
in that file are two change log files and a copyright file.

Jerry

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Upgrade Python from 3.5.3 to latest version

Wed Dec 12, 2018 2:24 am

On my not-quite-updated Raspberry Pi, installed python3.5 packages are at /usr/lib/python3.5/dist-packages/ if installed by apt, and at /usr/local/lib/python3.5/dist-packages/ if installed by pip.
If you run

Code: Select all

>>> import site

Code: Select all

>>> site.getsitepackages()
then you should get a list of all the package installation paths.
Look through them for smbus. Try reinstalling the package if you can't find anything.

Code: Select all

>>> site.__doc__
will give you information about installation paths, if you are interested.

User avatar
bensimmo
Posts: 4187
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Upgrade Python from 3.5.3 to latest version

Wed Dec 12, 2018 10:00 am

Try your code in the general environment, by default Python3 on Raspbian everything is system wide.

Try just
python3 myprog.py
smbus etc should be fine.

Your virtual environment will need all the modules installing within it. ( --system-site-packages is supposed to add them ? )
https://docs.python.org/3/library/venv.html
Though I'm not the best to ask about that, I'm lazy and keep it simple and you use the global setup

'sudo' will cause it to be installed/setup globally and not available to your benchmarks iirc.

If you need something from 3.6 or 3.7, then maybe look at installing it.
If not, don't bother, avoid the hassle and live the simpler life :-)

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Wed Dec 12, 2018 4:33 pm

Thanks to all of your help and with the addition of information in this thread viewtopic.php?t=122260, I think I understand the smbus problem and may have resolved it. It appears to be an issue with the distribution itself.

Using:

Code: Select all

sudo find / -name *smbus*
I located the file /usr/lib/python3/dist-packages/smbus.cpython-35m-arm-linux-gnueabihf.so and copied it to
/usr/lib/python3/dist-packages/smbus.so

now >>>import smbus works with system python and any subsequent venv's that I create.

Alternatively smbus2 library is working also. I have given smbus2 library a test with some code that remotely controls the key pad in my security system so am fairly confident in it as a replacement for smbus. I will revert back to smbus supplied with the distribution and give that a try too.

Either way I'm a happy camper with regard to the smbus issue. The last remaining hurdle is the spidev issue and I see it is being covered in another current thread viewtopic.php?f=32&t=228238, so I will study what's going on there and see if I can get spidev figured out.

Again thanks for the help - Jerry

jerryk
Posts: 30
Joined: Wed Nov 02, 2016 3:26 pm

Re: Upgrade Python from 3.5.3 to latest version

Thu Dec 13, 2018 8:36 am

I may have confused why smbus is now working. I have created a virtual environment two ways

Case One is:

Code: Select all

python3 -m venv venv/
Case Two is:

Code: Select all

python3 -m venv venv/ --system-site-packages
Case one is using home/pi/venv/pip inside the virtual environment and install all libraries to the virtual environment.

Case two is using /usr/bin/pip and installs libraries to system python distributions.

In case one >>import smbus returns a module not found error

in case two >>import smbus works fine.

My goal was to have a clean install with my projects working out virtual environments. No I'm not sure whether I want to use --system-site-packages if it installs packages outside of the virtual environment.

I must be seeing this wrong is my guess. Sorry for so many questions too, but I sure do appreciate all help given.

Jerry

Return to “Python”