ROS compatible python2 and use python3


System Version: ubuntu16.04

Update python3.5 to python3.7

download

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz

Decompression

tar zxvf Python-3.7.1rc2.tgz
cd Python-3.7.1rc2

Compile

./configure
sudo make
sudo make install

Python3.7 interpreter go first position, generally
/usr/local/bin/python3.7

Delete the original soft connection

rm -rf /usr/bin/python3
rm -rf /usr/bin/pip3

Add python3 soft link

ln -s /usr/local/bin/python3.7 /usr/bin/python3
#添加 pip3 的软链接
ln -s /usr/local/bin/pip3.7 /usr/bin/pip3

Issue notes

One problem: error ModuleNotFoundError: No module named '_ctypes'
solution

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install libssl-dev openssl
sudo apt-get install libffi-dev

问题二: zipimport.ZipImportError: can’t decompress data; zlib not available

Solution sudo apt-get install zlib

Question three: exception handling require TLS / SSL locations that appear

 sudo apt-get install gcc libffi-devel zlib* openssl-devel

Then you can re-edit and installation python7

Create a virtual environment that can run at the same time compatible python2 python3

Following in Ubuntu 16.04 x64 and kinetic test by ROS in
fact, just add shebang python3 in .py files, rosrun time will automatically call the appropriate interpreter:

#!/usr/bin/env python3

However, when installing ros-kinetic-desktop-full, but also did not python3 related package installation, you need to install it manually.

Creating virtualenv environment

sudo pip install virtualenv
# 我在这使用的是Python3.6rc2,指向系统已安装的python3也可以。
virtualenv -p /usr/bin/python3 venv
source venv/bin/activate
python -V
# Python 3.6.0rc2

virtualenv -p /usr/bin/python3 venv
This command creates a venv folder, put him ROS space to work, when the source of what we can use
as shown below
Here Insert Picture Description
questions : implementation of the above command is likely to encounter
problems a: python ModuleNotFoundError: ' CommandNotFound '
solutions into the next:

Check /usr/lib/command-not-found file, it is trying to find module
CommandNotFound, which is NOT shipped with python3.7 but DOES with
Ubuntu under directory /usr/lib/python3/dist-packages. What we need to
do is adding the directory to python import path:
vi .bashrc (or .profile) add line
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages:/usr/lib/python3/dist-packages
Pay attention that you may need to change the
/usr/local/lib/python3.7/site-packages to your own directory.
exit
and source .bashrc (or .profile)

Second problem: No module named'lsb_release '
resolve into the next:

python3.7 after the installation prompts
No module named'lsb_release '
modify / usr / bin / lsb_release
#! / usr / bin / python3.5m -ES

NOTE

source ros_test/devel/setup.zsh
# 如没有使用virtualenv,下一语句则跳过
source rosenv/bin/activate
rosrun py3test pytest.py//-如果要兼容python2,此语句不要执行

Note that, virtualenv even used in, python points to python3, but not as long as .py shebang add python3 file, rosrun still calls the python2. This can be easily compatible Package Python2 of the original.

Published 34 original articles · won praise 2 · Views 2332

Guess you like

Origin blog.csdn.net/weixin_44088559/article/details/105116697
Recommended