Whenever as a programmer we start a new project (in any language), we are required to set up an environment for our project to run. The environment may include things such as editor, packages supporting our project, language-specific linter and formatter, etc. A stable environment helps you to focus on the main aspects of implementation and leaving everything else with the setup to handle. Let's get started setting up a Python environment in vscode.
Before we being, please note that I have chosen vscode because I am used to working with it and you might have a different choice. It is totally fine as long as it helps you to enjoy your coding experience.
Setting up an environment for any language can be prescribed as the following points:
- Installing language-specific compiler/interpreter
- Installing a package manager
- Setting up Virtual Environment
- Setting up the code editor
For python, you will have to install a python Operating system specific interpreter to be able to execute your code. Just visit this link and install the appropriate version of python in your machine. Also, make sure you have correctly installed it on your system by the following command:
$ python --version
pip is a very popular python package installer. It helps you to manage your python packages. You can visit this link to install
pip. Again, just verify if you already have it installed on your system
$ pip --version
Python applications will often use packages and modules that don't come as part of the standard library (i.e. by the above step). Applications will sometimes need a specific version of a library. It means that there might be multiple applications with different versions of python and/or modules required to run the application. Having one global version being used by all application will not suffice the needs.
The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
# installing virtual environment
$ pip install virtualenv
$ virtualenv --version
Until now, we have been installing everything globally. From now on we will be creating a virtual environment that will restrict the installation to that specific environment (folder).
# creating a project folder
$ mkdir python-demo
# creating a virtual environment for this project
$ virtualenv python-demo
# this will create a virtual environment folder <python-demo> in the current folder
Once we have created a virtual environment, we need to make sure we install all other python packages for this project inside this project. This is done via activating virtual environment with the following command:
# for ubuntu
$ source <virtual-env-folder-path>/bin/activate
$ source python-demo/bin/activate
# for windows
# After this, your command prompt/terminal will change the path with the virtual environment name
# to deactivate the virtual environment, just type the command deactivate
(python-demo) $ deactivate
Now, let's move to set up a python environment in the vscode code editor. There are possibly 2 tasks a code editor should perform whenever you write code in it - Linting and Formatting the code. Vscode supports multiple linters and formatters to help you out in your workspace.
Before we create a workspace setting for our python environment, let's install a linter and a formatter for python. I will be using
pylint for the same. You can choose any other python package of your choice, just follow this link
# Make sure you have activated your virtual environment
(python-demo) $ pip install autopep8 pylint
I have created a
settings.json for the python environment in vscode.
Follow this gif to update your vscode editor
Do not forget to replace your virtual environment path with
Lastly, install this vscode python extension to enable python support in vscode.
You can also add debugger configuration in your vscode workspace by following this link.
If you find this helpful or have any suggestions, feel free to comment. Also, do not forget to hit ❤️ or 🦄 if you like my post.
See ya! until my next post 😋