The commands on the following page has been performed and tested with the following versions:
Start by updating your system:
sudo apt update
sudo apt upgrade
Install Visual Studio Code with Ubuntu Software. Other methods can be used, but Ubuntu Software remains the more stable option.
Python is already installed on Ubuntu. But be carrefull, two versions coexist on ubuntu: Python 2.7.17 and Python 3.6.9. The default version is the older one. I advise against using this old version. Instead, I strongly recommand using virtual environments with Python 3.
Install the Python paquet manager pip:
sudo apt install python-pip
or
sudo apt install python3-pip
Install virtualenv with the following command:
pip3 install virtualenv
Create a virtual environment for your project and specify this environment will use Python 3.
virtualenv ~/path/to/project -p /usr/bin/python3
You'll have to run the previous command everytime you'll create a new Python project. Activate your environment:
source ~/path/to/project/bin/activate
The name of the environment should appear at the beginning of your command line:
Validate that everything is fine by checking the versions:
(python3) $ python --version
Python 3.6.9
(python3) $ pip --version
pip 20.0.2 from /home/philippe/sources/python3/lib/python3.6/site-packages/pip (python 3.6)
You can deactivate the virtual environment with the following command:
deactivate
Launch Visual Studio Code. In File
>Open folder
[Ctrl-K Ctrl-O], sélect the
folder where you created your virtual environment. Create a Python script named
version.py
and add the following code:
import sys
print("___________________")
print("PYTHON VERSION")
print (sys.version)
print("___________________")
print("VERSION INFO")
print (sys.version_info)
Run your script by hitting CTRL-F5
. Visual Studio Code should ask you if you want to install
pylint
for checking code quality and suggest best practices. I strongly recommand to accept and
install pylint
.
You script should display something like:
___________________
PYTHON VERSION
3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]
___________________
VERSION INFO
sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)
Python 3 is running in your virtual environment in Visual Studio Code. You can
even set break points and debug your application by hitting F5
instead of Ctrl-F5
.
Enjoy!