{"id":7180,"date":"2018-01-15T17:11:28","date_gmt":"2018-01-15T17:11:28","guid":{"rendered":"https:\/\/blog.jetbrains.com\/pycharm\/?p=5326"},"modified":"2024-01-17T15:40:56","modified_gmt":"2024-01-17T14:40:56","slug":"micropython-plugin-for-pycharm","status":"publish","type":"pycharm","link":"https:\/\/blog.jetbrains.com\/pt-br\/pycharm\/2018\/01\/micropython-plugin-for-pycharm","title":{"rendered":"Announcing the MicroPython Plugin for PyCharm"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Today we\u2019ve released the <\/span><a href=\"https:\/\/github.com\/vlasovskikh\/intellij-micropython\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">MicroPython plugin<\/span><\/a><span style=\"font-weight: 400;\"> 1.0 for PyCharm. This plugin lets you edit your MicroPython code and interact with your MicroPython-powered microcontrollers using PyCharm. It supports ESP8266, Pyboard, and BBC Micro:bit devices. The plugin is being developed as a <\/span><a href=\"https:\/\/confluence.jetbrains.com\/display\/ALL\/JetBrains+on+GitHub\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">team project<\/span><\/a><span style=\"font-weight: 400;\"> by the PyCharm Community lead Andrey Vlasovskikh. The source code for the project can be found on <\/span><a href=\"https:\/\/github.com\/vlasovskikh\/intellij-micropython\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">GitHub<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">MicroPython is a relatively new member of the Python interpreters family. It\u2019s basically a Python 3.5 implementation designed for microcontrollers \u2014 small computing devices that are used everywhere from smart watches to cars. People usually program microcontrollers in C or an assembly language due to low performance and memory limits. Thanks to clever optimization techniques implemented in MicroPython you can now use (almost) standard Python for microcontrollers. For example, you can create your own Internet of Things device and program it in MicroPython.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The MicroPython plugin is compatible with both PyCharm Community and Professional editions. We&#8217;re going to make it available for IntelliJ IDEA soon as well. Let me walk you through the setup process and the features of the plugin using PyCharm:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We\u2019ll be using an ESP8266-based device called <\/span><a href=\"https:\/\/wiki.wemos.cc\/products:d1:d1_mini\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">WEMOS D1 mini<\/span><\/a><span style=\"font-weight: 400;\">. Basically, it\u2019s a Wi-Fi chip with a couple of digital and analog I\/O pins to connect external sensors and actuators. But for our simple demo, we won\u2019t need anything besides the LED light that is already located on the device and is connected to the digital output pin 2.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is our demo program which toggles the LED every second:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-title=\"\">import utime\r\nfrom machine import Pin\r\n\r\n\r\ndef main():\r\n    led = Pin(2, Pin.OUT)\r\n    enabled = False\r\n    while True:\r\n        if enabled:\r\n            led.off()\r\n        else:\r\n            led.on()\r\n        utime.sleep_ms(1000)\r\n        enabled = not enabled\r\n\r\n\r\nif __name__ == &#039;__main__&#039;:\r\n    main()\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Let\u2019s get started setting up our device!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">First of all, make sure your OS can see your device via USB. This step is device-dependent. For WEMOS D1 mini on Windows and macOS you\u2019ll need a serial port driver <\/span><a href=\"https:\/\/wiki.wemos.cc\/downloads\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">provided by the device vendor<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Next, we\u2019ll setup PyCharm to work with your device. First, you need to install the MicroPython plugin in PyCharm <\/span><i><span style=\"font-weight: 400;\">\u201cFile | Settings | Plugins\u201d<\/span><\/i><span style=\"font-weight: 400;\">. Then you need to create a new Python project in <\/span><i><span style=\"font-weight: 400;\">\u201cFile | New Project\u2026\u201d<\/span><\/i><span style=\"font-weight: 400;\">. In PyCharm 2017.3 the new project dialog with the correct settings will look like this:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-5344\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-image6.png\" alt=\"image6\" width=\"777\" height=\"460\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Make sure you\u2019ve configured a Python 3.5 or a newer interpreter for it (preferably a virtual environment), since the MicroPython plugin will later ask you to install a few Python packages to communicate with your device. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">After that add a new file to your new project with the contents of our program above. Finally, enable MicroPython support for your project in <\/span><i><span style=\"font-weight: 400;\">\u201cFile | Settings | Languages &amp; Frameworks | MicroPython\u201d<\/span><\/i><span style=\"font-weight: 400;\"> and specify your device there:<\/span><\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-configurable.png\" rel=\"attachment wp-att-5333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-5333\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-configurable.png\" alt=\"MicroPython Configurable\" width=\"886\" height=\"351\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Now let\u2019s see what the plugin has to offer.<\/span><\/p>\n<h2>Code Completion and Documentation<\/h2>\n<p><span style=\"font-weight: 400;\">The MicroPython plugin provides code completion and documentation for MicroPython-specific modules:<\/span><\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-code-completion.png\" rel=\"attachment wp-att-5332\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-5332\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-code-completion.png\" alt=\"MicroPython Code Completion\" width=\"887\" height=\"258\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Notice that code completion is context-aware. On this screenshot PyCharm shows you only the members of the <\/span><span style=\"font-weight: 400;\">utime<\/span><span style=\"font-weight: 400;\"> module.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The quick documentation window contains the docs for the selected name. Use Ctrl+Q (F1 on macOS) to show this pop-up window. You can also dock it and enable <\/span><i><span style=\"font-weight: 400;\">\u201cAuto-update from Source\u201d<\/span><\/i><span style=\"font-weight: 400;\"> to keep it permanently.<\/span><\/p>\n<p><span style=\"color: #000000; font-size: 1.8em;\">Syntax Checking and Type Checking<\/span><\/p>\n<p>The plugin searches syntax errors and other problems in your code like potential <code md-src-pos=\"3196..3212\">AttributeError<\/code> or <code md-src-pos=\"3216..3229\">ImportError<\/code> using static code analysis. It comes with Python stub files for device-specific binary modules. These stubs contain Python type hints that make it possible to check types in your MicroPython code:<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-type-checking.png\" rel=\"attachment wp-att-5336\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-5336\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-type-checking.png\" alt=\"MicroPython Type Checking\" width=\"886\" height=\"286\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">On the screenshot above you can see several Python syntax errors when the user tries to write some C code in the middle of their Python file. There is also a type error in\u00a0<\/span><code md-src-pos=\"3638..3660\">utime.sleep_ms(3.14)<\/code>, since this function only accepts integers.<\/p>\n<h2>Flash Files to Devices<\/h2>\n<p><span style=\"font-weight: 400;\">The MicroPython plugin helps you to upload your files to your MicroPython device via USB. Use <\/span><i><span style=\"font-weight: 400;\">\u201cMicroPython\u201d<\/span><\/i><span style=\"font-weight: 400;\"> run configurations to flash files or folders to your device in <\/span><i><span style=\"font-weight: 400;\">\u201cRun | Edit Configurations\u2026\u201d<\/span><\/i><span style=\"font-weight: 400;\"> menu. To quickly upload a single file you can select <\/span><i><span style=\"font-weight: 400;\">\u201cRun \u2018Flash &lt;your-file-name&gt;.py&#8217;\u201d<\/span><\/i><span style=\"font-weight: 400;\"> from the context menu of your Python file:<\/span><\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-run.png\" rel=\"attachment wp-att-5335\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-5335\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-run.png\" alt=\"MicroPython Run Configuration\" width=\"887\" height=\"389\" \/><\/a><\/p>\n<h2>MicroPython REPL<\/h2>\n<p><span style=\"font-weight: 400;\">Interactive experiments play an important role in Python development, but they are even more important with microcontrollers which usually don\u2019t have any screens to show possible errors. The MicroPython plugin allows you to quickly run an interactive Python REPL console. Use <\/span><i><span style=\"font-weight: 400;\">\u201cTools | MicroPython | MicroPython REPL\u201d<\/span><\/i><span style=\"font-weight: 400;\"> menu to run a MicroPython shell on your device.<\/span><\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-repl.png\" rel=\"attachment wp-att-5334\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-5334\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/01\/pycharm-repl.png\" alt=\"MicroPython REPL\" width=\"886\" height=\"390\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">I hope you enjoy this plugin. I\u2019ll be glad to hear your feedback and how you\u2019re using it. Tell me about your experience with it in the comments below, or on twitter: <\/span><a href=\"https:\/\/twitter.com\/vlasovskikh\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">@vlasovskikh<\/span><\/a><span style=\"font-weight: 400;\"> or <\/span><a href=\"https:\/\/twitter.com\/pycharm\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">@pycharm<\/span><\/a><span style=\"font-weight: 400;\">. Star or fork the <\/span><a href=\"https:\/\/github.com\/vlasovskikh\/intellij-micropython\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">intellij-micropython<\/span><\/a><span style=\"font-weight: 400;\"> repository on GitHub, send your issues and pull requests!<\/span><\/p>\n","protected":false},"author":1152,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","categories":[808,1065,2347],"tags":[91],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/7180"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/pycharm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/1152"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=7180"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/7180\/revisions"}],"predecessor-version":[{"id":435026,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/7180\/revisions\/435026"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=7180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=7180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=7180"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=7180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}