Python development with Visual Studio Code
VS Code is an light but powerful editor developed by Microsoft. I have been using this editor for Python and embedded development(using PlatformIO) for a few months now.
Using PlatformIO with VSCode is rather simple - just install it through built in package manager and you're good to go. However python development needs a bit more work to setup properly. Especially if you want to develop Django apps with VSCode. That is what I'll be covering here in this post.
Configure Python virtual environment
Of course you could use globally installed Python, however having independent virtual environments for each project or experiment brings a lot of benefits. For example, you can always spin a fresh virtual environment, install required modules, test if your code works as expected and release it. Or you could just copy existing virtual environment, update modules to their newest versions and then test if your project still works as expected before upgrading those modules on your production environment. Even if you only want to experiment with python, it is always a good idea to start from a new virtual environment. At least I have acquired a habit of creating a new virtual python environment every time I start a project...
virtualenv
So, if we want to work with virtual environments, we need to install virtualenv package first. virtualenv is a tool used to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
1pip install virtualenv
You can test your installation using this command:
1virtualenv --version
To create a virtual environment inside your current directory:
1virtualenv my_project
This will create a directory my_project inside your current directory with all the necessary executables, modules and so on using your current python installation. In case you have more than one python version installed, you can choose whatever python version you want like this:
1virtualenv -p /path/to/your/python/executable/python2.7 my_project
To activate your newly created virtual environment on Linux:
1source my_project/bin/activate
or for Windows(You have to first enable it by configuring set-executionpolicy to e.g. remotesigned):
1my_project\bin\activate.ps1
This will replace environment variables in such a way, that your current python interpreter along with all the installed modules will become the ones from your virtual environment.
virtualenv wrapper
There is a wrapper around virtualenv, that allows for much more easier management of virtual environments. This tool is called virtualenvwrappervirtualenvwrapper and virtualenvwrapper-win for Linux and Windows environments respectively. Installation is easy using pip. Usage is also very easy. In order to create a virtual environment use:
1mkvirtualenv my_project
Virtual environment will be automatically activated. When you want to exit the virtual environment:
1deactivate
If you want to select another virtual environment:
1workon my_project_1
There are other commands and I encourage you to go to the virtualenvwrapper page and learn about them.
pipenv
There is another alternative to virtualenv or virtualenvwrapper that is called pipenv which is a dependency manager for Python projects. Pipenv manages Python modules on a per-project basis. Before installing any packages pipenv creates a Pipfile, creates a virtual environment and installs the packages:
1cd my_project<br />pipenv install requests
This will create a Pipfile which is used to track dependencies for your project, much like requirements.txt file that you populate with pip freeze
.
I personally prefer virtualenvwrapper over other options.
Configure VSCode for python
After you have your virtual environment, you can begin configuring VSCode. First of all you need to install Python for VSCode. To do that, go to Extensions section and type "python" into search box. The very first entry reading Python will be the package we're looking for:
You can go to Settings->Settings and start configuring python from there, but that would setup python globally for every project. I would suggest having a settings file template somewhere and edit .vscode/settings.json file every time you create a new project. That would allow having independent virtual environments for each project. When you create a project, VSCode creates .vscode directory inside your project directory and creates settings.json file there which is empty. By the way, since I'm mainly developing from Windows, all my paths will be Windows-style. You should change the paths to the ones relevant for your installation. Linux paths are a bit different, but the principle remains.
So, after you've got your project in VSCode, go to Settings->Settings, and select Workspace Settings. You should see something like this:
Then copy&paste these values on the right:
1{
2 "python.pythonPath": "c:\\Users\\FoxIS\\Envs\\itohi\\Scripts\\python.exe", // virtual environment python binary
3 "python.linting.pep8Enabled": true,
4 "python.linting.pylintPath": "c:\\Users\\FoxIS\\Envs\\itohi\\Scripts\\pylint.exe", // virtual environment pylint binary
5 "python.linting.pylintArgs": [
6 "--load-plugins",
7 "pylint_django"
8 ],
9 "python.linting.pylintEnabled": true,
10 "editor.rulers": [
11 80,
12 120
13 ],
14 "launch": {
15 "version": "0.2.0",
16 "configurations": [
17 {
18 "name": "Django",
19 "type": "python",
20 "request": "launch",
21 "stopOnEntry": true,
22 "pythonPath": "${config:python.pythonPath}",
23 "program": "${workspaceRoot}/manage.py",
24 "cwd": "${workspaceRoot}",
25 "args": [
26 "runserver",
27 "--noreload",
28 "--nothreading"
29 ],
30 "env": {},
31 "envFile": "c:\\Users\\FoxIS\\Envs\\itohi", //virtualenv path, change to whatever suits your system
32 "debugOptions": [
33 "WaitOnAbnormalExit",
34 "WaitOnNormalExit",
35 "RedirectOutput",
36 "DjangoDebugging"
37 ]
38 }
39 ]
40 }
41}
Then of course change the paths to python that are valid on your machine. In order to use pep8 and pylint, install those modules on your virtualenv:
1pip install autopep8
2pip install pylint
Now you should be ready to develop Python code and run Django development server from VSCode! :)
PS. You can enter launch configuration into a separate launch.json file instead of the workspace configuration ("launch" json key). Also it is possible to instruct VSCode to automatically create launch.json file for you with already predefined python configurations. Just go to Debug section and select cog icon and select Python: