{"id":36565,"date":"2018-07-30T08:00:45","date_gmt":"2018-07-30T08:00:45","guid":{"rendered":"https:\/\/fedoramagazine.org\/?p=21995"},"modified":"2018-07-30T08:00:45","modified_gmt":"2018-07-30T08:00:45","slug":"how-to-use-vs-code-for-your-python-projects","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2018\/07\/30\/how-to-use-vs-code-for-your-python-projects\/","title":{"rendered":"How to use VS Code for your Python projects"},"content":{"rendered":"<p>Visual Studio Code, or VS Code<em>,<\/em> is an open source code editor that also includes tools for building and debugging an application. With the Python extension enabled,\u00a0<em>vscode<\/em> becomes a great working environment for any Python developer. This article shows you which extensions are useful, and how to configure VS Code\u00a0to get the most out of it.<\/p>\n<p><span id=\"more-21995\"><\/span><\/p>\n<p>If you don&#8217;t have it\u00a0installed, check out our previous article, <a href=\"https:\/\/fedoramagazine.org\/using-visual-studio-code-fedora\/\">Using Visual Studio Code on Fedora<\/a>:<\/p>\n<blockquote class=\"wp-embedded-content\">\n<p><a href=\"https:\/\/fedoramagazine.org\/using-visual-studio-code-fedora\/\">Using Visual Studio Code on Fedora<\/a><\/p>\n<\/blockquote>\n<h3>Install the VS Code Python extension<\/h3>\n<p>First, to make VS Code Python friendly, install the Python extension from the marketplace.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-22088\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vs-code-for-your-python-projects.gif\" alt=\"\" width=\"1023\" height=\"765\" \/><\/p>\n<p>Once the Python extension installed, you can now configure the Python extension.<\/p>\n<p>VS Code manages its configuration inside JSON files. Two files are used:<\/p>\n<ul>\n<li>One for the global settings that applies to all projects<\/li>\n<li>One for project specific settings<\/li>\n<\/ul>\n<p>Press <strong>Ctrl+,<\/strong> (comma) to open the global settings.<\/p>\n<h4>Setup the Python Path<\/h4>\n<p>You can configure VS Code to automatically select the best Python interpreter for each of your projects. To do this, configure the <em>python.pythonPath<\/em>\u00a0key in the global settings.<\/p>\n<pre class=\"prettyprint\">\/\/ Place your settings in this file to overwrite default and user settings. { \"python.pythonPath\":\"${workspaceRoot}\/.venv\/bin\/python\", }<\/pre>\n<p>This sets\u00a0VS Code to use the Python interpreter located in the project root directory under the <em>.venv<\/em> virtual environment directory.<\/p>\n<h4>Use environment variables<\/h4>\n<p>By default,\u00a0VS Code uses environment variables defined in the project root directory in a <em>.env<\/em>\u00a0file. This is useful to set environment variables like:<\/p>\n<pre class=\"prettyprint\">PYTHONWARNINGS=\"once\"<\/pre>\n<p>That setting ensures that warnings are displayed when your program is running.<\/p>\n<p>To change this default, set the <em>python.envFile <\/em>configuration key as follows:<\/p>\n<div>\n<pre class=\"prettyprint\">\"python.envFile\": \"${workspaceFolder}\/.env\",<\/pre>\n<\/div>\n<h3>Code Linting<\/h3>\n<p>The Python extension also supports different code linters (<em>pep8<\/em>, <em>flake8<\/em>, <em>pylint<\/em>). To enable your favorite linter, or the one used by the project you&#8217;re working on, you need to set a few configuration items.<\/p>\n<p>By default\u00a0<em>pylint <\/em>is enabled. But for this example, configure <em>flake8:<\/em><\/p>\n<div>\n<pre class=\"prettyprint\">\"python.linting.pylintEnabled\": false, \"python.linting.flake8Path\": \"${workspaceRoot}\/.venv\/bin\/flake8\", \"python.linting.flake8Enabled\": true, \"python.linting.flake8Args\": [\"--max-line-length=90\"],<\/pre>\n<\/div>\n<p>After enabling the linter, your code is underlined to show where it doesn&#8217;t meet criteria enforced by the linter. Note that for this example to work, you need to install <em>flake8<\/em> in the virtual environment of the project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-22093\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vs-code-for-your-python-projects-1.gif\" alt=\"\" width=\"586\" height=\"247\" \/><\/p>\n<h3>Code Formatting<\/h3>\n<p>VS Code also lets you configure automatic code formatting. The extension currently supports <em>autopep8<\/em>, <em>black<\/em> and <em>yapf<\/em>. Here&#8217;s how to configure <em>black<\/em>.<\/p>\n<div>\n<pre class=\"prettyprint\">\"python.formatting.provider\": \"black\", \"python.formatting.blackPath\": \"${workspaceRoot}\/.venv\/bin\/black\" \"python.formatting.blackArgs\": [\"--line-length=90\"], \"editor.formatOnSave\": true,<\/pre>\n<\/div>\n<p>If you don&#8217;t want the editor to format your file on save,\u00a0 set the option to <em>false<\/em> and use <strong>Ctrl+Shift+I<\/strong> to format the current document. Note that for this example to work, you need to install <em>black<\/em> in the virtual environment of the project.<\/p>\n<h3>Running Tasks<\/h3>\n<p>Another great feature of VS Code is that it can run tasks. These tasks are also defined in a JSON file saved in the project root directory.<\/p>\n<h4>Run a development flask server<\/h4>\n<p>In this example, you&#8217;ll create a task to run a Flask development server. Create a new Build using the basic template that can run an external command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-22100\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vs-code-for-your-python-projects-2.gif\" alt=\"\" width=\"768\" height=\"401\" \/><\/p>\n<p>Edit the <em>tasks.json<\/em> file as follows to create a new task that runs the <em>Flask<\/em> development server:<\/p>\n<div>\n<pre class=\"prettyprint\">{ \/\/ See https:\/\/go.microsoft.com\/fwlink\/?LinkId=733558 \/\/ for the documentation about the tasks.json format \"version\": \"2.0.0\", \"tasks\": [ { \"label\": \"Run Debug Server\", \"type\": \"shell\", \"command\": \"${workspaceRoot}\/.venv\/bin\/flask run -h 0.0.0.0 -p 5000\", \"group\": { \"kind\": \"build\", \"isDefault\": true } } ] }<\/pre>\n<\/div>\n<p>The Flask development server uses an environment variable to get the entrypoint of the application. Use the <em>.env <\/em>file to declare these variables. For example:<\/p>\n<pre>FLASK_APP=wsgi.py FLASK_DEBUG=True<\/pre>\n<p>Now you can execute the task using <strong>Ctrl+Shift+B<\/strong>.<\/p>\n<h3>Unit tests<\/h3>\n<p>VS Code also has the\u00a0unit test runners\u00a0<em>pytest, unittest,\u00a0<\/em>and <em>nosetest<\/em>\u00a0integrated out of the box. After you enable a test runner, VS Code discovers the unit tests and letsyou to run them individually, by test suite, or simply all the tests.<\/p>\n<p>For example, to enable <em>pytest:<\/em><\/p>\n<div>\n<pre>\"python.unitTest.pyTestEnabled\": true, \"python.unitTest.pyTestPath\": \"${workspaceRoot}\/.venv\/bin\/pytest\",<\/pre>\n<\/div>\n<p>Note that for this example to work, you need to install <em>pytest<\/em> in the virtual environment of the project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-22110\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vs-code-for-your-python-projects-3.gif\" alt=\"\" width=\"648\" height=\"512\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio Code, or VS Code, is an open source code editor that also includes tools for building and debugging an application. With the Python extension enabled,\u00a0vscode becomes a great working environment for any Python developer. This article shows you which extensions are useful, and how to configure VS Code\u00a0to get the most out of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":36566,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[45,42,46,47],"class_list":["post-36565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fedora-os","tag-fedora","tag-for-developers","tag-magazine","tag-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/36565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=36565"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/36565\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/36566"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=36565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=36565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=36565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}