Using Pycharm ============= .. toctree:: :maxdepth: 1 Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` Introduction ------------ Once you start writing anything but very simple scripts, learning to use an IDE can greatly improve your productivity in both coding and debugging Python applications. This section will provide a quick overview of the features of PyCharm, an IDE that is pre-installed on the PythonEDU VM. .. tip:: One thing you will start to notice with PYCharm is that it has a huge array of shortcuts to access its features. However, you don't need to memorize these shortcuts to use PyCharm effectively. You will slowly start to memorize the ones you find you are using frequently. The infrequently used ones can still be accessed from the main menu, the tool bar, or the context menu. .. _section_heading-Creating_A_Project: Creating A Project ------------------ PyCharm, like other IDE's, thinks in terms of projects, which are the container for your code and the various settings and metadata that are part of it. If the welcome screen is shown, select :guilabel:`Create New Project`. If the IDE is shown, select :guilabel:`File` -> :guilabel:`New Project`. This will open the project creation dialog. Then do the following: * Select the :guilabel:`Pure Python` project template type. * Change the location to where you want to create the project (this also sets its name) * Choose your interpreter: * By default a virtualenv is pre-selected. However, for this lesson, click the drop down arrow, select :guilabel:`Existing Interpreter`, and select one from the :guilabel:`Interpreter` drop down list. * Click :guilabel:`Create`. .. _section_heading-Adding_Source_Files: Adding Source Files ------------------- If you created your project using a directory that already had source files, PyCharm would offer to add them to your project for you. However, for this lesson, there are no pre-existing source code files so let's make one. To add a new source file to your project, do the following: * :guilabel:`File` -> :guilabel:`New` -> :guilabel:`Python File` * Give the new file a name. For this lesson, call it, "plot_sin_wave.py". * Click :guilabel:`Ok` For this lesson, paste the following into the editor window. .. literalinclude:: plot_sin_wave.py :linenos: .. _section_heading-PyCharm_UI_Tour: PyCharm UI Tour --------------- After a project has been created the full UI becomes visible and with some source code present, we can now take a tour of the UI. #. Main menu #. Toolbar #. Projects side bar #. Structure side bar #. Console #. Terminal #. Editor tabs #. Left gutter - Line numbers #. Left gutter - Cold folding #. Left gutter - Breakpoints #. Right gutter - Lens view of errors and introspection warnings .. _figure-PyCharm_UI: .. figure:: pycharm_ui.png :scale: 100 % :align: center PyCharm UI .. _section_heading-Debugging_An_App: Debugging An App ---------------- PyCharm includes a debugger which allows you to run your application, step through the lines of code and inspect variables in real time. To start debugging your application, you must first create a configuration that tells PyCharm things like: * What script you want to run * What directory to run the script in * What interpreter to use * What arguments to pass the script To create a configuration, do the following: * :guilabel:`Run` -> :guilabel:`Edit Configurations` * Click the "+" icon, on the upper-left of the window,2 and select :guilabel:`Python` * Choose a name for this configuration using the :guilabel:`name` field (e.g. "plot_sin_wave") * Under :guilabel:`Script path`, click the button with the ellipses icon and find the plot_sin_wave.py file. * Ensure an interpreter is selected under :guilabel:`Python interpreter` The best part about using the debugger is being able to stop code execution at a line of code in order to inspect the variable values. To set a breakpoint, do the following: * Inside the left gutter, click on the line you want to set a breakpoint on. A small red circle should appear. PyCharm can execute your app in 2 ways: 1. In "run" mode, by clicking run icon (the play arrow) or typing :kbd:`Alt+Shift+F10`. Your app executes at full speed but debugging is not possible. 2. In "debug" mode, by clicking the debug icon (the bug) or typing :kbd:`Alt+Shift+F9`. When the debugger has stopped on a line of code (a.k.a. break mode), PyCharm allows you to view the values of the variables that are in scope in the different threads and stack frames that exist. You can drag variables to the watch window to keep the variable visible no matter which thread or stack frame you select. In the code window: * PyCharm will display the value of the names on the left of any assignment statements. * If you hover your mouse over a variable, PyCharm will display its value in a tool tip pop-up. To continue executing lines of code, you can do the following: * Clicking the Step Over button (or :kbd:`F8`), will cause PyCharm will execute the current line of code, including any function calls, and stop at the next executable statement. * Clicking the Step Into button (or :kbd:`F7`), will cause PyCharm to execute the current line of code and stop at the next executable statement. If the current line of code is a function call, code execution will stop just inside the function. * Clicking the Resume Program button (or :kbd:`F5`), will cause PyCharm to resume running your program until the next breakpoint is encountered. Any output from your application will be sent to the Console window. .. _section_heading-Extras: Extras ------ You can get a PDF list of PyCharm shortcuts from the following: :guilabel:`Help` -> :guilabel:`Keymap Reference`