![]() |
Fedora - How to use VS Code for your Python projects - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Computers (https://www.sickgaming.net/forum-86.html) +--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html) +--- Thread: Fedora - How to use VS Code for your Python projects (/thread-85845.html) |
Fedora - How to use VS Code for your Python projects - xSicKxBot - 07-31-2018 How to use VS Code for your Python projects <div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/how-to-use-vs-code-for-your-python-projects.gif" width="1023" height="765" title="" alt="" /></div><div><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, <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 to get the most out of it.</p> <p><span id="more-21995"></span></p> <p>If you don’t have it installed, check out our previous article, <a href="https://fedoramagazine.org/using-visual-studio-code-fedora/">Using Visual Studio Code on Fedora</a>:</p> <blockquote class="wp-embedded-content"> <p><a href="https://fedoramagazine.org/using-visual-studio-code-fedora/">Using Visual Studio Code on Fedora</a></p> </blockquote> <h3>Install the VS Code Python extension</h3> <p>First, to make VS Code Python friendly, install the Python extension from the marketplace.</p> <p><img 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> <p>Once the Python extension installed, you can now configure the Python extension.</p> <p>VS Code manages its configuration inside JSON files. Two files are used:</p> <ul> <li>One for the global settings that applies to all projects</li> <li>One for project specific settings</li> </ul> <p>Press <strong>Ctrl+,</strong> (comma) to open the global settings.</p> <h4>Setup the Python Path</h4> <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> key in the global settings.</p> <pre class="prettyprint">// Place your settings in this file to overwrite default and user settings. { "python.pythonPath":"${workspaceRoot}/.venv/bin/python", }</pre> <p>This sets VS Code to use the Python interpreter located in the project root directory under the <em>.venv</em> virtual environment directory.</p> <h4>Use environment variables</h4> <p>By default, VS Code uses environment variables defined in the project root directory in a <em>.env</em> file. This is useful to set environment variables like:</p> <pre class="prettyprint">PYTHONWARNINGS="once"</pre> <p>That setting ensures that warnings are displayed when your program is running.</p> <p>To change this default, set the <em>python.envFile </em>configuration key as follows:</p> <div> <pre class="prettyprint">"python.envFile": "${workspaceFolder}/.env",</pre> </div> <h3>Code Linting</h3> <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’re working on, you need to set a few configuration items.</p> <p>By default <em>pylint </em>is enabled. But for this example, configure <em>flake8:</em></p> <div> <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> </div> <p>After enabling the linter, your code is underlined to show where it doesn’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> <p><img 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> <h3>Code Formatting</h3> <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’s how to configure <em>black</em>.</p> <div> <pre class="prettyprint">"python.formatting.provider": "black", "python.formatting.blackPath": "${workspaceRoot}/.venv/bin/black" "python.formatting.blackArgs": ["--line-length=90"], "editor.formatOnSave": true,</pre> </div> <p>If you don’t want the editor to format your file on save, 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> <h3>Running Tasks</h3> <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> <h4>Run a development flask server</h4> <p>In this example, you’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> <p><img 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> <p>Edit the <em>tasks.json</em> file as follows to create a new task that runs the <em>Flask</em> development server:</p> <div> <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> </div> <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> <pre>FLASK_APP=wsgi.py FLASK_DEBUG=True</pre> <p>Now you can execute the task using <strong>Ctrl+Shift+B</strong>.</p> <h3>Unit tests</h3> <p>VS Code also has the unit test runners <em>pytest, unittest, </em>and <em>nosetest</em> integrated 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> <p>For example, to enable <em>pytest:</em></p> <div> <pre>"python.unitTest.pyTestEnabled": true, "python.unitTest.pyTestPath": "${workspaceRoot}/.venv/bin/pytest",</pre> </div> <p>Note that for this example to work, you need to install <em>pytest</em> in the virtual environment of the project.</p> <p><img 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> </div> |