Switch the default python version (to solve the problem that the default version of python in ROS is python2)

1 Introduction

After installing ROS, many small partners need to write ROS programs based on python3 (especially to deploy deep learning algorithms), but the default python version of ROS is python2, which makes it incompatible with some algorithms written based on python3. Some friends will choose to use anaconda to create a python3 virtual environment, but doing so may cause conflicts between the virtual environment and the default environment of ROS. Therefore, it is not recommended to share anaconda and ROS. The easiest way is to switch the default python version of the system to python3 , and then proceed with a series of operations such as pip to configure the environment.

2. Method

2.1 View the current system default python version

Execute the following command, you can see that the default python version should be python2.7.

python -V

2.2 Configure different python versions

Execute the following command, the number behind represents the priority, the larger the number, the higher the priority, here you want python3 to have a higher priority, so set the priority of python3 to 2.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

2.3 Choose a different python version

Execute the following command.

sudo update-alternatives --config python

When the following interface appears, you can switch between different python versions, and the options in the red circle are to be selected. For example, if you want to switch back to python2, enter 1 and press Enter; if you want to switch to python3, enter 2 and press Enter.

 3. Summary

Through this method, you can freely switch between python2 and 3, which is very convenient.

Guess you like

Origin blog.csdn.net/weixin_52514564/article/details/129160842