-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyCharm.html
463 lines (417 loc) · 48 KB
/
PyCharm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>PyCharm</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/bootstrap-sphinx.css?v=fadd4351" />
<link rel="stylesheet" type="text/css" href="_static/custom.css?v=77160d70" />
<script src="_static/documentation_options.js?v=a8da1a53"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="CLion" href="CLion.html" />
<link rel="prev" title="Visual Studio Build Impact" href="VisualStudioBuildImpact.html" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59110517-1', 'auto');
ga('send', 'pageview');
</script>
</head><body>
<div id="navbar" class="navbar navbar-default ">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.mantidproject.org">
</a>
<span class="navbar-text navbar-version pull-left"><b>main</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="index.html">Home</a></li>
<li><a href="https://download.mantidproject.org">Download</a></li>
<li><a href="https://docs.mantidproject.org">User Documentation</a></li>
<li><a href="http://www.mantidproject.org/contact">Contact Us</a></li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="index.html">Documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">PyCharm</a></li>
</ul>
</div> </p>
</div>
<div class="container">
<div class="row">
<div class="body col-md-12 content" role="main">
<section id="pycharm">
<span id="pycharm-ref"></span><h1>PyCharm<a class="headerlink" href="#pycharm" title="Link to this heading">¶</a></h1>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#selecting-pycharm-versions" id="id1">Selecting PyCharm versions</a></p></li>
<li><p><a class="reference internal" href="#setup-python-development-environment-with-conda" id="id2">Setup Python development environment with Conda</a></p></li>
<li><p><a class="reference internal" href="#debug-python-in-workbench" id="id3">Debug Python in Workbench</a></p></li>
<li><p><a class="reference internal" href="#debug-python-in-unit-tests" id="id4">Debug Python in unit tests</a></p></li>
<li><p><a class="reference internal" href="#debug-python-in-system-tests-remote-debugging" id="id5">Debug Python in system tests/remote debugging</a></p></li>
</ul>
</nav>
<section id="selecting-pycharm-versions">
<h2><a class="toc-backref" href="#id1" role="doc-backlink">Selecting PyCharm versions</a><a class="headerlink" href="#selecting-pycharm-versions" title="Link to this heading">¶</a></h2>
<p>There are two versions of PyCharm that are in use in the team, Community and Professional. The main difference for our workflow is that professional offers support for remote debugging which can help with debugging Python code that is called from C++, such as algorithms called from Python interfaces.</p>
<p>If you haven’t installed PyCharm yet do that now, PyCharm can be installed from <a class="reference external" href="https://jetbrains.com/pycharm/download/">here</a>.</p>
</section>
<section id="setup-python-development-environment-with-conda">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">Setup Python development environment with Conda</a><a class="headerlink" href="#setup-python-development-environment-with-conda" title="Link to this heading">¶</a></h2>
<p>The assumption has been made that you have setup and built Mantid already, if you have not, please do so before hand by following, <a class="reference external" href="GettingStarted/GettingStarted.html">this guide</a>.</p>
<p>At any point in these instructions where <code class="docutils literal notranslate"><span class="pre">DebugWithRelRuntime</span></code> is used (including in file paths),
you can replace it with any other build type such as <code class="docutils literal notranslate"><span class="pre">Debug</span></code> or <code class="docutils literal notranslate"><span class="pre">Release</span></code>.
We use <code class="docutils literal notranslate"><span class="pre">DebugWithRelRuntime</span></code> for Conda specific builds to allow debugging due to <code class="docutils literal notranslate"><span class="pre">Debug</span></code> not being functional with the <code class="docutils literal notranslate"><span class="pre">Release</span> <span class="pre">ABIs</span></code>.</p>
<ul>
<li><p>Open PyCharm</p></li>
<li><p>If no project has been selected already, open the Mantid source code as a project.</p></li>
<li><p>Open the <code class="docutils literal notranslate"><span class="pre">File->Settings</span> <span class="pre">menu</span></code>.</p></li>
<li><p>In the left hand side select the option that is <code class="docutils literal notranslate"><span class="pre">Project:</span> <span class="pre">{SOURCE_CODE_FOLDER_NAME}</span></code> for example <code class="docutils literal notranslate"><span class="pre">Project:</span> <span class="pre">mantid</span></code>.</p></li>
<li><p>Select the <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Interpreter</span></code> option.</p></li>
<li><p>Click <code class="docutils literal notranslate"><span class="pre">Add</span> <span class="pre">Interpreter</span></code> on the top right, then <code class="docutils literal notranslate"><span class="pre">Add</span> <span class="pre">Local</span> <span class="pre">Interpreter...</span></code>.</p></li>
<li><p>From the left side of the window select <code class="docutils literal notranslate"><span class="pre">Conda</span> <span class="pre">Environment</span></code>.</p></li>
<li><p>Add the path to your Conda executable, e.g. <code class="docutils literal notranslate"><span class="pre">C:\Users\<username>\AppData\Local\mambaforge\Scripts\conda.exe</span></code> and click <code class="docutils literal notranslate"><span class="pre">Load</span> <span class="pre">Environments</span></code>.</p></li>
<li><p>Click the <code class="docutils literal notranslate"><span class="pre">Use</span> <span class="pre">Existing</span> <span class="pre">environment</span></code> radio button and select the <code class="docutils literal notranslate"><span class="pre">mantid-developer</span></code> environment in the drop down list.</p></li>
<li><p>Click OK to close the window.</p></li>
<li><p>Ensure that next to <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Interpreter:</span></code> it says <code class="docutils literal notranslate"><span class="pre">mantid-developer</span></code>. You will also see a list of the python packages installed in your mantid-developer environment.</p></li>
<li><p>Then click Apply.</p></li>
<li><p>Back on the left side, under <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Interpreter</span></code> there should be an option for <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Structure</span></code>. Select that.</p></li>
<li><p>If you do not build Mantid in the same directory as your source but somewhere else add this as another Content Root, by selecting <code class="docutils literal notranslate"><span class="pre">+</span> <span class="pre">Add</span> <span class="pre">Content</span> <span class="pre">Root</span></code> on the right hand side now.</p></li>
<li><p>In the file tree, select each of the following and mark them as source directories by clicking the <code class="docutils literal notranslate"><span class="pre">Sources</span></code> button while they are selected:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/scripts</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/Framework/PythonInterface</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/qt/applications/workbench</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/qt/widgets</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/qt/python/mantidqt</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{SOURCE}/qt/python/mantidqtinterfaces</span></code></p></li>
</ul>
</div></blockquote>
</li>
<li><p>In the file tree select your build directory and mark as excluded by clicking the <code class="docutils literal notranslate"><span class="pre">Excluded</span></code> button whilst it’s selected.</p></li>
<li><p>On Windows, select the <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin/DebugWithRelRuntime</span></code> directory and mark as source by clicking the <code class="docutils literal notranslate"><span class="pre">Sources</span></code> button whilst it’s selected.</p></li>
<li><p>On Linux or MacOS, select your <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin</span></code> directory and mark as source by clicking the <code class="docutils literal notranslate"><span class="pre">Sources</span></code> button whilst it’s selected.</p></li>
<li><p>Click Apply, and then OK to close the window.</p></li>
</ul>
</section>
<section id="debug-python-in-workbench">
<span id="debug-workbench-in-pycharm-ref"></span><h2><a class="toc-backref" href="#id3" role="doc-backlink">Debug Python in Workbench</a><a class="headerlink" href="#debug-python-in-workbench" title="Link to this heading">¶</a></h2>
<p>Now that your Python development environment has been setup we can setup the debugging using Workbench.</p>
<ul class="simple">
<li><p>With an open project in Pycharm, open the Play configuration menu by Opening <code class="docutils literal notranslate"><span class="pre">Run->Edit</span> <span class="pre">Configurations...</span></code>.</p></li>
<li><p>Click the <code class="docutils literal notranslate"><span class="pre">+</span></code> icon top left</p></li>
<li><p>Select Python</p></li>
<li><p>Name it something to do with <code class="docutils literal notranslate"><span class="pre">Workbench</span></code></p></li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Script</span> <span class="pre">Path:</span></code> box, on Linux/MacOS enter the <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin/workbench</span></code> Python script, on Windows enter <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin/DebugWithRelRuntime/workbench-script.pyw</span></code>, <code class="docutils literal notranslate"><span class="pre">.pyw</span></code> files will not appear in the search window as it only shows <code class="docutils literal notranslate"><span class="pre">.py</span></code> files, so you cannot search for it with the GUI.</p></li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Parameters</span></code> box add <code class="docutils literal notranslate"><span class="pre">--single-process</span></code> so that the multiprocess startup is disabled and breakpoints can be attached to the primary process. See the <a class="reference internal" href="Workbench/RunningWorkbench.html#runningworkbench"><span class="std std-ref">Running Workbench</span></a> documentation for more information.</p></li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Working</span> <span class="pre">directory:</span></code> box, on Linux/MacOS enter the <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin</span></code> directory, on Windows enter <code class="docutils literal notranslate"><span class="pre">{BUILD}/bin/DebugWithRelRuntime</span></code> directory.</p></li>
<li><p>Ensure the <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Interpreter:</span></code> box is set to use your <code class="docutils literal notranslate"><span class="pre">(mantid-developer)</span></code> Conda environment.</p></li>
<li><p>Click OK to save and exit the window.</p></li>
<li><p>You can now click the green play button in the top right of the window to create a Workbench instance from pycharm.</p></li>
<li><p>Alternatively you can click the green bug next to the green play button to start a debug session.</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>On Windows, you may see the following error when launching workbench in debug mode:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>incompatible<span class="w"> </span>copy<span class="w"> </span>of<span class="w"> </span>pydevd<span class="w"> </span>already<span class="w"> </span>imported:
C:<span class="se">\P</span>rogram<span class="w"> </span>Files<span class="se">\J</span>etBrains<span class="se">\P</span>yCharm<span class="w"> </span>Community<span class="w"> </span>Edition<span class="w"> </span><span class="m">2023</span>.1.2<span class="se">\p</span>lugins<span class="se">\p</span>ython-ce<span class="se">\h</span>elpers<span class="se">\p</span>ydev<span class="se">\_</span>pydev_bundle<span class="se">\_</span>_init__.py
C:<span class="se">\P</span>rogram<span class="w"> </span>Files<span class="se">\J</span>etBrains<span class="se">\P</span>yCharm<span class="w"> </span>Community<span class="w"> </span>Edition<span class="w"> </span><span class="m">2023</span>.1.2<span class="se">\p</span>lugins<span class="se">\p</span>ython-ce<span class="se">\h</span>elpers<span class="se">\p</span>ydev<span class="se">\_</span>pydev_bundle<span class="se">\_</span>pydev_calltip_util.py
...
</pre></div>
</div>
<p>To resolve the error, remove <strong>only</strong> the debugpy package from your Conda environment with</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>conda<span class="w"> </span>remove<span class="w"> </span>debugpy<span class="w"> </span>--force
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">--force</span></code> argument tells Conda to remove the single package only, ignoring the packages that depend on debugpy.
Note that the Mamba implementation of <code class="docutils literal notranslate"><span class="pre">remove</span> <span class="pre">--force</span></code> did not skip dependency checking until version 1.5.2 (October 2023).
If your version of Mamba is older than this, use the Conda command.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>macOS developers will see the warning from Qt in the terminal:</p>
<p><code class="docutils literal notranslate"><span class="pre">An</span> <span class="pre">OpenGL</span> <span class="pre">surfcace</span> <span class="pre">format</span> <span class="pre">was</span> <span class="pre">requested</span> <span class="pre">that</span> <span class="pre">is</span> <span class="pre">either</span> <span class="pre">not</span> <span class="pre">version</span> <span class="pre">3.2</span> <span class="pre">or</span> <span class="pre">higher</span> <span class="pre">or</span> <span class="pre">a</span> <span class="pre">not</span> <span class="pre">Core</span> <span class="pre">Profile.</span></code>
<code class="docutils literal notranslate"><span class="pre">Chromium</span> <span class="pre">on</span> <span class="pre">macOS</span> <span class="pre">will</span> <span class="pre">fall</span> <span class="pre">back</span> <span class="pre">to</span> <span class="pre">software</span> <span class="pre">rendering</span> <span class="pre">in</span> <span class="pre">this</span> <span class="pre">case.</span></code>
<code class="docutils literal notranslate"><span class="pre">Hardware</span> <span class="pre">acceleration</span> <span class="pre">and</span> <span class="pre">features</span> <span class="pre">such</span> <span class="pre">as</span> <span class="pre">WebGL</span> <span class="pre">will</span> <span class="pre">not</span> <span class="pre">be</span> <span class="pre">available.</span></code></p>
<p>It can be safely ignored but is present as a reminder of some deprecated OpenGL
functionality being used.
It is only visible to developers and the spelling mistake is real.</p>
</div>
</section>
<section id="debug-python-in-unit-tests">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Debug Python in unit tests</a><a class="headerlink" href="#debug-python-in-unit-tests" title="Link to this heading">¶</a></h2>
<p>This section assumes you have followed all previous instructions for debugging Python in workbench</p>
<p>There are 2 main ways to debug Python unit tests using the Unittests Python module.</p>
<ol class="arabic">
<li><p>Navigate to the test you want to run in PyCharm, on the left side of the file in the margin, just to the right of the line numbers there should be a green play button, click that and it will let you start a debug or normal run of the tests.</p></li>
<li><dl>
<dt>A little more involved, but is easier to expand and test many things at once.</dt><dd><ul>
<li><p>Same as when creating the Workbench debug session. Open the configuration menu by navigating to <code class="docutils literal notranslate"><span class="pre">Run->Edit</span> <span class="pre">Configurations...</span></code></p></li>
<li><p>Click the <code class="docutils literal notranslate"><span class="pre">+</span></code> icon top left</p></li>
<li><p>Select Unittests</p></li>
<li><p>Give an appropriate name for the section of code you will be testing</p></li>
<li><p>You have 3 options, enter the module name, script path or custom.</p>
<blockquote>
<div><ul class="simple">
<li><p>Module name for testing workbench project recovery tests looks like this <code class="docutils literal notranslate"><span class="pre">workbench.projectrecovery</span></code>, this runs all of the tests in the project recovery section. This is very useful for testing all of a specific section of the code base, without running it in a terminal.</p></li>
<li><p>Script path is very similar instead of passing a module name, you just give a filepath such as <code class="docutils literal notranslate"><span class="pre">{SOURCE}/qt/applications/workbench/workbench/projectrecovery</span></code> this achieves exactly the same as the previous step.</p></li>
<li><p>Custom is for passing custom arguments to the Unittests executable such as these: <a class="reference external" href="https://docs.python.org/3/library/unittest.html#command-line-interface">https://docs.python.org/3/library/unittest.html#command-line-interface</a></p></li>
</ul>
</div></blockquote>
</li>
</ul>
</dd>
</dl>
</li>
</ol>
</section>
<section id="debug-python-in-system-tests-remote-debugging">
<h2><a class="toc-backref" href="#id5" role="doc-backlink">Debug Python in system tests/remote debugging</a><a class="headerlink" href="#debug-python-in-system-tests-remote-debugging" title="Link to this heading">¶</a></h2>
<p>This functionality is useful for debugging Python code that is spawned in separate threads, such as Python algorithms called from C++ and system tests.</p>
<p>A PyCharm Professional license is required to use the Remote Debugging feature.</p>
<p>This section assumes you have followed all previous instructions for debugging Python in workbench and unit tests.</p>
<ul>
<li><p>Like the Unit tests and workbench we need to add it as a configuration, open the configuration menu by navigating to <code class="docutils literal notranslate"><span class="pre">Run->Edit</span> <span class="pre">Configurations...</span></code></p></li>
<li><p>Click the <code class="docutils literal notranslate"><span class="pre">+</span></code> icon top left</p></li>
<li><p>Select <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Debug</span> <span class="pre">Server</span></code></p></li>
<li><p>Give an appropriate name for remote debugging such as <code class="docutils literal notranslate"><span class="pre">Remote</span> <span class="pre">Debugging</span></code></p></li>
<li><p>Copy the snippet of code that consists of <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">pydevd-pycharm</span></code></p></li>
<li><p>Close the configuration window</p></li>
<li><p>Open Terminal at the bottom of the PyCharm window</p></li>
<li><p>Paste the snippet of code and hit enter, this will install the remote debugger for PyCharm to use.</p></li>
<li><p>Once installed, re-open the configuration menu by navigating to <code class="docutils literal notranslate"><span class="pre">Run->Edit</span> <span class="pre">Configurations...</span></code></p></li>
<li><p>Ensure that the previously created <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Debug</span> <span class="pre">Server</span></code> is selected in the left hand side tree selection.</p></li>
<li><p>Ensure that you set the port box to something that isn’t 0 and isn’t in use by your system at present such as <code class="docutils literal notranslate"><span class="pre">8080</span></code>.</p></li>
<li><p>Copy the snippet of Python code that looks like this:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pydevd_pycharm</span>
<span class="n">pydevd_pycharm</span><span class="o">.</span><span class="n">settrace</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">8080</span><span class="p">,</span> <span class="n">stdoutToServer</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">stderrToServer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p>Paste this code where you want to start debugging from, this will act like a breakpoint during normal debugging.</p></li>
<li><p>Click the drop down menu next to the play icon in the top right. Select the <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">Debug</span> <span class="pre">Server</span></code> you configured, then click the debug next to the play icon.</p></li>
<li><p>Run the python code that you want to debug, for example run the system tests, and it will pause execution on where you pasted your remote debug code earlier.</p></li>
<li><p>Any new breakpoints can be added like normal but they must come after the remote code snippet pasted earlier.</p></li>
</ul>
</section>
</section>
<section id="legacy-and-not-maintained-past-this-point-only-use-if-explicitly-not-using-conda">
<h1>Legacy and not maintained past this point (Only use if explicitly not using Conda)<a class="headerlink" href="#legacy-and-not-maintained-past-this-point-only-use-if-explicitly-not-using-conda" title="Link to this heading">¶</a></h1>
<section id="setting-up-pycharm-on-windows">
<h2>Setting up PyCharm on Windows<a class="headerlink" href="#setting-up-pycharm-on-windows" title="Link to this heading">¶</a></h2>
<ol class="arabic">
<li><p>Once PyCharm is open, set up the project. Go to <code class="docutils literal notranslate"><span class="pre">File->Open</span></code> and select the root directory in which both your source and build directories reside.</p>
<p>Go to <code class="docutils literal notranslate"><span class="pre">File->Settings</span></code>, then under <code class="docutils literal notranslate"><span class="pre">Project</span></code> you will set two sub-menus <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Interpreter</span></code> and <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Structure</span></code>. The interpreter defines the Python executable that will be used to run your code, and the structure menu allows you to decide which folders within the project to include and index.</p>
</li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Interpreter</span></code> sub menu, at the top select the options button and click <code class="docutils literal notranslate"><span class="pre">Add...</span></code>, a new window should appear titled “Add Python Interpreter”. In the menu on the left, if you are using Conda select “Conda Environment”, if you haven’t set up Conda follow the Getting Started guidance for it, select existing environment and if not present already put in the path to your Python interpreter, and your conda executable. Alternatively select “System Interpreter” (a version of Python with all the correct variables set already exists within Mantid, if you are not using Conda). Click on the <code class="docutils literal notranslate"><span class="pre">...</span></code> to open a file browser, and navigate to;</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/external/src/ThirdParty/lib/Python3.8/Python.exe
</pre></div>
</div>
<p>This is the interpreter, so select “Ok” and apply the changes. This should bring up a list of all the packages associated to the interpreter. There should be many packages, however you should not see PyQt (but instead QtPy).</p>
</li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Structure</span></code> sub menu you should see your root directory with the source/build directories both visible (if not, add them). The folder structure should be present in the centre of the window allowing you to mark folders orange (excluded) or blue (source). Source directories will be searched for Python code.</p>
<p>Within the source directory add the following to your sources:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/scripts
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/Framework/PythonInterface
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/applications/workbench
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/widgets
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/Python
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/external/src/ThirdParty/lib
</pre></div>
</div>
<p>If you are writing scripts in any other directories, you can also mark them as sources. This helps PyCharm give better auto-complete and import suggestions during development.</p>
<p>Additionally, in the Mantid build directory add the following as source folders:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Build<span class="w"> </span>Directory>/bin/Debug
</pre></div>
</div>
<p>here we are setting up PyCharm for the Debug build, you would use <code class="docutils literal notranslate"><span class="pre">/bin/Release</span></code> instead if you are building mantid in release mode.</p>
</li>
<li><p>The environment needs to be set up before running the configuration. Follow the instructions below to use either the EnvFile plugin (recommended) or manual path setup.</p></li>
</ol>
<p>NOTE : In some cases, imports in the code will still be highlighted red when they come from folders within the <code class="docutils literal notranslate"><span class="pre">script/</span></code> folder, or from other folders entirely. To fix this simply add the relevant folder that contains the module you are importing in the same fashion as step 3 above.</p>
</section>
<section id="running-files-in-the-debugger-with-envfile-extension">
<span id="pycharm-debugging-env-file"></span><h2>Running Files in the Debugger with EnvFile extension<a class="headerlink" href="#running-files-in-the-debugger-with-envfile-extension" title="Link to this heading">¶</a></h2>
<p>Do not run files in the debugger with EnvFile extension with Conda, as Conda does this job for you.</p>
<p>Running Python code from within PyCharm which depends on the Python API, or PyQt for example requires one extra step. Because the source root labelling from the previous section only affects PyCharm searching and not the run configuration, before running the file we must set up the run configuration correctly.</p>
<ol class="arabic simple" start="4">
<li><p>Install the EnvFile plugin by Borys Pierov. The plugin can be installed in multiple ways:</p>
<ol class="loweralpha simple">
<li><p>Open Settings(CTRL + SHIFT + S), to go Plugins and search for <code class="docutils literal notranslate"><span class="pre">EnvFile</span></code>. Install and restart PyCharm.</p></li>
<li><p>Go to the plugin’s <a class="reference external" href="https://plugins.jetbrains.com/plugin/7861-envfile">webpage</a>, download and install it.</p></li>
</ol>
</li>
<li><p>To edit the configurations go to Run->Run… and select Edit Configurations. Notice that there is now a <code class="docutils literal notranslate"><span class="pre">EnvFile</span></code> tab under the configuration’s name.
- Note that you have to do that for each configuration, or you can change the template configuration, and all configuration that use that template will have the EnvFile setup.</p></li>
<li><p>Open the <code class="docutils literal notranslate"><span class="pre">EnvFile</span></code> tab, check <code class="docutils literal notranslate"><span class="pre">Enable</span> <span class="pre">EnvFile</span></code> and <code class="docutils literal notranslate"><span class="pre">Substitute</span> <span class="pre">Environmental</span> <span class="pre">Variables</span> <span class="pre">(...)</span></code> - this allows setting up the third-party paths dynamically.</p></li>
<li><p>Click the <code class="docutils literal notranslate"><span class="pre">+</span></code> (plus) on the right side, select the <code class="docutils literal notranslate"><span class="pre">pycharm.env</span></code> file in the root of the <strong>build</strong> directory.</p></li>
</ol>
<p>For running the Workbench continue onto <a class="reference external" href="https://docs.mantidproject.org/nightly/workbench/index.html#workbench" title="(in MantidProject v6.11)"><span>Workbench</span></a>, and follow the instructions to set up the <em>Script Path</em> and <em>Working Directory</em>.</p>
<p>Advantages of this approach:</p>
<ul class="simple">
<li><p>You can have multiple instances of PyCharm running with environment configuration for separate repositories. This is otherwise not possible, as all PyCharm instances seem to share a parent process and environment. (as is the case of 11/01/2019, it might change in the future)</p></li>
<li><p>This makes possible switching projects for multiple repositories via the File > Open Recent … menu, as when the new project is opened its environment won’t be poluted with environment variables from the last one.</p>
<ul>
<li><p>This can cause errors when the external dependencies aren’t quite the same between all the repositories, as some packages might be missing, or be different versions.</p></li>
</ul>
</li>
</ul>
<p>Disadvantages:</p>
<ul class="simple">
<li><p>Additional setup for each configuration necessary. Thankfully, if the template is edited to have the correct <code class="docutils literal notranslate"><span class="pre">EnvFile</span></code> setup, all copies of it will have it too. Copying an already existing configuration also copies the <code class="docutils literal notranslate"><span class="pre">EnvFile</span></code> setup.</p></li>
</ul>
</section>
<section id="running-files-in-the-debugger-without-envfile-extension">
<h2>Running Files in the Debugger without EnvFile extension<a class="headerlink" href="#running-files-in-the-debugger-without-envfile-extension" title="Link to this heading">¶</a></h2>
<p>This can be done in two ways:</p>
<ul>
<li><p>Open PyCharm using <code class="docutils literal notranslate"><span class="pre">pycharm.bat</span></code> which can be found in the build directory (this sets some additional environment variables compared with simply opening PyCharm directly).</p>
<ul class="simple">
<li><p>This is preferred if you only have 1 repository with which PyCharm is used. If you need to use PyCharm on multiple repositories, it is recommended that you use the EnvFile extension.</p></li>
</ul>
</li>
<li><p>To edit the configurations go to <code class="docutils literal notranslate"><span class="pre">Run->Run...</span></code> and select <code class="docutils literal notranslate"><span class="pre">Edit</span> <span class="pre">Configurations</span></code>. This should open up a sub window. Hit the green <code class="docutils literal notranslate"><span class="pre">+</span></code> in the top left to create a new configuration and name it. In order to tell PyCharm where to look for Python modules and libraries we need to add some folders to the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> environment variable. Click on the <code class="docutils literal notranslate"><span class="pre">...</span></code> next to the <em>Environment Variables</em> box, and hit the <code class="docutils literal notranslate"><span class="pre">+</span></code> icon. In the Name column enter “PATH”, in the value column enter the following;</p>
<blockquote>
<div><div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Build<span class="w"> </span>Directory><span class="se">\b</span>in<span class="se">\D</span>ebug<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\b</span>in<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\b</span>in<span class="se">\m</span>ingw<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\l</span>ib<span class="se">\P</span>ython3.8<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\l</span>ib<span class="se">\q</span>t5<span class="se">\p</span>lugins<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\l</span>ib<span class="se">\q</span>t5<span class="se">\b</span>in<span class="p">;</span>
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory><span class="se">\e</span>xternal<span class="se">\s</span>rc<span class="se">\T</span>hirdParty<span class="se">\l</span>ib<span class="se">\q</span>t5<span class="se">\l</span>ib<span class="p">;</span>
%PATH%
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<p>The semi-colon delimited list of paths should end in <code class="docutils literal notranslate"><span class="pre">;%PATH%</span></code> so that we prepend to the existing list of paths rather than overwriting them.</p>
<p>You should now be able to run and debug the scripts using the newly created configuration, by adding the full path of the file in the <code class="docutils literal notranslate"><span class="pre">Script</span> <span class="pre">path</span></code> box at the top of the configuration window.</p>
<p>As an example, create a new file in <code class="docutils literal notranslate"><span class="pre"><Mantid</span> <span class="pre">Source</span> <span class="pre">Directory>/scripts/</span></code> called <code class="docutils literal notranslate"><span class="pre">test.py</span></code>. Copy into it the Python code below.</p>
</section>
<section id="testing-using-pyqt">
<h2>Testing using PyQt<a class="headerlink" href="#testing-using-pyqt" title="Link to this heading">¶</a></h2>
<p>To test that the above instructions have worked, you can simply create a new Python file with the following content (for PyQt5)</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Check that PyQt imports</span>
<span class="kn">from</span> <span class="nn">qtpy</span> <span class="kn">import</span> <span class="n">QtCore</span><span class="p">,</span> <span class="n">QtGui</span><span class="p">,</span> <span class="n">QtWidgets</span>
<span class="c1"># Check that the Mantid Python API imports</span>
<span class="kn">import</span> <span class="nn">mantid.simpleapi</span>
<span class="k">class</span> <span class="nc">DummyView</span><span class="p">(</span><span class="n">QtWidgets</span><span class="o">.</span><span class="n">QWidget</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">DummyView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">grid</span> <span class="o">=</span> <span class="n">QtWidgets</span><span class="o">.</span><span class="n">QGridLayout</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="n">btn</span> <span class="o">=</span> <span class="n">QtWidgets</span><span class="o">.</span><span class="n">QPushButton</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">grid</span><span class="o">.</span><span class="n">addWidget</span><span class="p">(</span><span class="n">btn</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">QtWidgets</span><span class="o">.</span><span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
<span class="n">ui</span> <span class="o">=</span> <span class="n">DummyView</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">)</span>
<span class="n">ui</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">())</span>
</pre></div>
</div>
</section>
<section id="local-debugging-of-unit-tests-with-pycharm">
<h2>Local Debugging of Unit Tests with PyCharm<a class="headerlink" href="#local-debugging-of-unit-tests-with-pycharm" title="Link to this heading">¶</a></h2>
<p>This <strong>does not</strong> require a PyCharm Professional license for debugging, but requires additional setup for running unit tests.</p>
<ol class="arabic simple">
<li><p>Go to your Run/Debug Configurations.</p></li>
<li><p>Open Templates > Python tests > Unittests configuration.</p></li>
<li><p>Set the working directory to <code class="docutils literal notranslate"><span class="pre"><Mantid</span> <span class="pre">Build</span> <span class="pre">Dir>/bin/Debug</span></code>, for a Debug build, or <code class="docutils literal notranslate"><span class="pre"><Mantid</span> <span class="pre">Build</span> <span class="pre">Dir>/bin/Release</span></code> for a Release build.</p></li>
<li><p>Add the EnvFile to the Unittests configuration, instructions in <a class="reference internal" href="#pycharm-debugging-env-file"><span class="std std-ref">Running Files in the Debugger with EnvFile extension</span></a>.</p></li>
<li><p>You should now be able to click the Run/Debug icons next to each unit test method or class to run/debug them.</p></li>
</ol>
</section>
<section id="setting-up-pycharm-on-linux">
<h2>Setting up PyCharm on Linux<a class="headerlink" href="#setting-up-pycharm-on-linux" title="Link to this heading">¶</a></h2>
<ol class="arabic">
<li><p>Use the native Python interpreter (<code class="docutils literal notranslate"><span class="pre">/usr/bin/Python3</span></code>) rather than from <code class="docutils literal notranslate"><span class="pre"><Mantid</span> <span class="pre">Source</span> <span class="pre">Directory>/external/src/ThirdParty/lib/Python3.8/Python.exe</span></code></p></li>
<li><p>In the <code class="docutils literal notranslate"><span class="pre">Project</span> <span class="pre">Structure</span></code> sub menu you should see your root directory with the source/build directories both visible (if not, add them). The folder structure should be present in the centre of the window allowing you to mark folders orange (excluded) or blue (source). Source directories will be searched for Python code.</p>
<p>Within the source directory add the following to your sources:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/scripts
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/Framework/PythonInterface
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/applications/workbench
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/widgets
<Mantid<span class="w"> </span>Source<span class="w"> </span>Directory>/qt/Python
</pre></div>
</div>
<p>If you are writing scripts in any other directories, you can also mark them as sources. This helps PyCharm give better auto-complete and import suggestions during development.</p>
<p>Additionally, in the Mantid build directory add the following as source folders:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><Mantid<span class="w"> </span>Build<span class="w"> </span>Directory>/bin/
</pre></div>
</div>
<p>It is recommended that you add the whole build folder to <code class="docutils literal notranslate"><span class="pre">excluded</span></code>. This will not interfere with the <code class="docutils literal notranslate"><span class="pre">bin</span></code> directory, inside the build, being used as a source folder. It will just limit the scope that PyCharm searches for files, classes, etc.</p>
</li>
<li><p>Go to Run->Run… and select Edit Configurations. Go to Templates > Python. Make <code class="docutils literal notranslate"><span class="pre"><Mantid</span> <span class="pre">Build</span> <span class="pre">Directory>/bin;</span></code> the <code class="docutils literal notranslate"><span class="pre">Working</span> <span class="pre">Directory</span></code>. This will then be used for all Python configurations you make.</p></li>
</ol>
</section>
<section id="useful-plugins">
<h2>Useful Plugins<a class="headerlink" href="#useful-plugins" title="Link to this heading">¶</a></h2>
<p>You can install non-default plugins by pressing <code class="docutils literal notranslate"><span class="pre">Ctrl+Alt+S</span></code> to open the <strong>Settings/Preferences</strong> dialog and then going to <strong>Plugins</strong>.
From here you can manage plugins, or add new ones by clicking <strong>Browse repositories</strong>.</p>
<p>The following non-default plugins are things our team has found useful for Mantid development:</p>
<ul class="simple">
<li><p><strong>Markdown support</strong> - Side by side rendering of markdown documents such as``.md`` , <code class="docutils literal notranslate"><span class="pre">.rst</span></code> (requires <a class="reference external" href="https://graphviz.gitlab.io/download/">Graphviz</a> to show graphs in preview)</p></li>
<li><p><strong>dotplugin</strong> - Syntax highlighting for <code class="docutils literal notranslate"><span class="pre">DOT</span></code></p></li>
<li><p><strong>BashSupport</strong> - Syntax highlighting for <code class="docutils literal notranslate"><span class="pre">BASH</span></code> scripts</p></li>
<li><p><strong>CMD Support</strong> - Syntax highlighting for <code class="docutils literal notranslate"><span class="pre">.BAT</span></code> ~scripts</p></li>
</ul>
<p>Please add to this list if you find a useful plugin of your own</p>
</section>
<section id="remote-development">
<h2>Remote Development<a class="headerlink" href="#remote-development" title="Link to this heading">¶</a></h2>
<p>Note: Requires PyCharm Professional.</p>
<p>PyCharm supports deployment and syncronisation of written code to a remote server via SSH.</p>
<p>Open a local copy of the project and then follow the guides here for <a class="reference external" href="https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html">configuring the remote interpreter</a> and <a class="reference external" href="https://www.jetbrains.com/help/pycharm/creating-a-remote-server-configuration.html">creating a deployment configuration</a>.</p>
</section>
</section>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<ul class="nav navbar-nav" style=" float: right;">
<li>
<a href="VisualStudioBuildImpact.html" title="Previous Chapter: Visual Studio Build Impact"><span class="glyphicon glyphicon-chevron-left visible-sm"></span><span class="hidden-sm hidden-tablet">« Visual Studio...</span>
</a>
</li>
<li>
<a href="CLion.html" title="Next Chapter: CLion"><span class="glyphicon glyphicon-chevron-right visible-sm"></span><span class="hidden-sm hidden-tablet">CLion »</span>
</a>
</li>
<li><a href="#">Back to top</a></li>
</ul>
<p>
</p>
</div>
</footer>
</body>
</html>