In PyCharm, it is possible to set a script that runs upon opening a new console (through Settings -> 'Build, Execution, Deployment' -> Console -> Python Console -> Starting script).
Is there a way to similarly apply a startup script to the debugger console? I find myself importing the same packages over and over again, each time I run the code.
When you run Python Console inside PyCharm, it executes custom PyCharm script at <PYCHARM_PATH>/plugins/python/helpers/pydev/pydevconsole.py.
On the other hand, when you run PyCharm Debug Console while debugging, it executes custom PyCharm script at <PYCHARM_PATH>/Plugins/python/helpers/pydev/pydevd.py with command line parameter --file set to script you are debugging.
You can modify pydevd.py file if you want (Apache 2 license), but the easier approach would be to create startup script in which you import modules you need, functions and such and import ALL while inside PyCharm Debug Console. This would reduce all your imports to one.
Walkthrough:
Let's create 2 files:
main.py - Our main script which we will debug
startup.py - Modules, functions or something else that we would like to import.
main.py content:
sentence = 'Hello Debugger'
def replace_spaces_with_hyphens(s):
return s.replace(' ', '-')
replace_spaces_with_hyphens(sentence) # <- PLACE BREAKPOINT!
When breakpoint is hit, this is what we have inside scope:
If you always find yourself importing some modules and creating some functions, you can define all that inside startup.py script and import everything as from startup import *.
startup.py:
# Example modules you always find yourself importing.
import random
import time
# Some function you always create because you need it.
def my_imported_function():
print("Imported !")
Inside Python Debugger Console, use from startup import * as mentioned above and you would see all modules and function inside scope, ready for use.
you could just create a new debug configuration (run > edit configurations) and point it to a script in your project (e.g. called debug.py that you gitignore). Then when you hit debug it will run that script and drop you into a console.
Personally, I prefer to just launch ipython in the embedded terminal than using the debug console. On linux, you can create a bash alias in your .bashrc such as alias debug_myproject=PYTHONSTARTUP=$HOME/myproject/debug.py ipython. Then calling debug_myproject will run that script and drop you into an ipython console.
Related
I am trying to run this GitHub project in python, but I could only run it using the Terminal of Pycharm IDE.
According to the guide from the GitHub repository, I removed the $ sign from the beginning of $ python train.py RGCN PPI and could run it there. What does $ mean here and how can I run a file like this in Python Console (for example after >>> sign)?
The '$' isn't part of Python's syntax, it's a visual cue in the documentation representing the command prompt.
To answer the question from the title of this post, I'll provide some
instructions first on how to load scripts into the Python console.
However, for your specific case, you don't need this. Scroll down to
the part about debugging in PyCharm.
There's two ways you can get your script into the console. One is to simply load it using the right version of the two lines I give right below, or you can load it as a module - even if it wasn't intended to be one.
In general, to execute a script in the Python shell on Python 2 you can do
>>> execfile(r"<path to script here>")
On Python 3 it's more verbose:
>>> exec(open(r"<path to script here>").read())
The effect this has is as if you cut-n-pasted the script into the console. The console's global scope will get all the functions, classes, and variables that are leftmost indented in the file. Also it might not run your if __name__ == '__main__': block. But you could hack that.
If you want the vars/classes/etc to be put in another scope than your console's global scope, then there are two additional parameters to the above commands. The first one is a dictionary for the globals , and one for the locals. You can get away with only supplying the globals parameter - it's just an ordinary dictionary object you need.
If the file you want to load is a module, you could import it as you would any other module by appending its home folder to the Python module search path, and using the import directive. You can load your script this way even if it wasn't intended to be module.
>>> import sys
>>> sys.path.append(r'/Users/todd/projects/mymodule_folder')
>>> import mymodule
If you make modifications to it and want to reload it:
>>> import importlib
>>> importlib.reload(mymodule)
Loading your script as a module avoids polluting your console's global scope. After it loads, just prefix the names of your script's functions and variables with the module name. The module name will be the name of the file without the .py extension.
If the script requires command line options, you could just hard code values for those into the script and disable lines of code that try and get values from the CLI. If it gets complicated, consider running it in an IDE as described in the next section.
So the above is how you can run your python scripts in whatever Python REPL console you want.
BUT loading your scripts into the Python console may not be at all
required for your purposes. You wanted to debug some scripts (train.py,
test.py) from this project:
https://github.com/microsoft/tf-gnn-samples).
Debugging Command Line Script With PyCharm
In many cases, a Python script is written to run from the OS shell and take command line options from the user. These kinds of script could be loaded into the Python console, but most require some minor hacks to run. However, if all you want to do is debug such a script, you don't need to muck with the console.
PyCharm supports running these as is (as does Eclipse and other IDEs) like any other script. It's just a matter of creating a run/debug configuration for the project. I just installed PyCharm and gave it a try in order to record the details. Easy task.
Just open the project in PyCharm, and above the editor pane, on the toolbar, there's a menu option for Edit Configurations. Click that to open the Run/Debug Configurations dialog and click the + to add a configuration. A small dialog will appear with predefined templates - select Python as your template and accept.
Then in the main dialog, fill in Script path: with the path to train.py (or another script), then click the checkbox, [x] Emulate terminal in output console. Also, you can add command line options in the Parameters: text box (I put in the text: mymodel mytask just to satisfy the script's need for two parameters). Click OK at the bottom to accept the configuration and shut the dialog.
Now you should see a green bug icon on the toolbar.Set a breakpoint in the __main__ block of the script and click the debug icon to start debugging the script. That should do it!
Debugging Python Command Line Script with PDB
PDB - the Python Debugger can be run without an IDE. This is another way to debug a script of any sort. If it requires command line parameters, provide them from the OS shell when you start the debugger:
$ pdb myscript.py mymodel mytask
That's really all there is to starting a debug session. PDB requires some knowledge of its text based commands. After starting a session, you can get a listing of code near the current line of execution by entering l. Enter help to see a listing of the commands.
To step one line of execution, enter 's' for step, or enter 'step'. To set a breakpoint, enter break <line-number>, or set a breakpoint on an expression. A reference on the commands available can be found here: https://docs.python.org/2/library/pdb.html . There are also plenty of versions of pdb cheatsheets available online - just google "pdb cheatsheet" and select one.
I am trying to set a breakpoint in an external Python module in VS code.
I have tried editing the source file and inserting import pdb; pdb.set_trace() where I want the breakpoint.
This enters the pdb command line debugger rather than the debugger in the VS Code GUI.
How do I set a breakpoint in an imported Python module so that I enter the VS Code debugger?
Marvin's suggestion appears to be sufficient:
add a launch configuration "justMyCode":false .
See code.visualstudio.com/docs/python/debugging#_justmycode
You need to import the folder containing the source code of the imported module into the project, using file -> add folder to workspace. In my case this was /Users/robinl/anaconda3/lib/python3.6/site-packages/great_expectations/
Within VS code, you can then navigate to the file you want to debug and set a breakpoint by clicking to the left of the code as normal.
I am trying to do what the user from this question is doing. I have it working for the separate IPython console but not the one integrated into PyCharm.
To sum up, I want IPython to import some modules when I start it. I have gone to the C:\Users\Name\.ipython\profile_default\startup directory and I have made a startup.py file which contains
from numpy import *
print 'NumPy imported succesfully!'
After setting PYTHONSTARTUP to point to the file, the IPython console outside of PyCharm works as intended, but the one in PyCharm does not.
You can go to File -> Settings -> Build, Execution, Deployment -> Console -> Python Console. There you have a field named "Starting script" which is executed when you launch the console.
I had to restart PyCharm for the changes to actually take place, but it worked.
Note : I use PyCharm 4.5.3 Community Edition.
Say I have a script that imports various modules in Python.
import my_module
from some_other_module import foo
...
I then run this script from IPython.
Say I make changes to the function bar in my_module and foo in some_other_module.
Say that I now want to interactively call either my_module.bar() or foo() from my IPython session.
Is there a way to have IPython automatically reload every loaded module when I invoke a command before executing the command?
If not automatically, how can I reload every loaded module manually in IPython without having to explicitly name the module?
Finally, is there a way to set up my IPython session in my ipython_config.py (startup file) so that it supports this functionality off-the-shelf?
I would suggest a %load_ext autoreload followed by a %autoreload? to see how to use it.
You can also have a look at InteractiveShellApp.extensions and InteractiveShellApp.extra_extension configuration options for extension at startup.
Finally, you can also add a .py file in your IPython profile dir ($ ipython locate to get it), put it in the startup subfolder, it will be executed at startup time.
There is a restriction though, C modules cannot be reloaded.
How do you go about debug Blender Python in Eclipse and PyDev?
What I have tried is:
http://www.luxrender.net/wiki/LuxBlend25_Debugging_with_eclipse
http://www.blender.org/forum/viewtopic.php?t=3914&sid=717a127d12596f89e4aea0c54938ef80
But non of then seams to work?
Regards
There is very good e-book written by Witold Jaworski about Blender add-on programming. It includes chapters with step by step instructions how to setup Eclipce with PyDev to debug Blender add-ons.
Programming Add-ons for Blender 2.5
Here is how I setup debug, which is slight different but based on the lux-render tutorial.
First, create the a .py file, lets call it debug.py, which will contain a function which we will call later to setup debugging. Put this file in the same folder as the main __init__.py of your module. As per the lux-renderer tutorial, add the following code, updating PYDEV_SOURCE_DIR.
import sys
def startdebug():
try:
# set the PYDEV_SOURCE_DIR correctly before using the debugger
PYDEV_SOURCE_DIR = 'C:\Program Files\eclipse\plugins\org.python.pydev.debug_2.5.0.2012040618\pysrc'
# test if PYDEV_SOURCE_DIR already in sys.path, otherwise append it
if sys.path.count(PYDEV_SOURCE_DIR) < 1:
sys.path.append(PYDEV_SOURCE_DIR)
# import pydevd module
import pydevd
# set debugging enabled
pydevd.settrace(None, True, True, 5678, False, False)
except:
pass
When setting the PYDEV_SOURCE_DIR ensure you point it to the org.python.pydev.debug_xxxxx. There is another folder similiar to this. To ensure you have the correct folder it will contain a /pysrc folder.
Now in your main __init__.py, this must come before any other import statements to work correctly. Add the following directly under the bl_info section, as strangely blender parses this itself.
DEBUGGING = True
if(DEBUGGING):
import debug
debug.startdebug()
Having it here will avoids adding per file traces like the lux-render tutorial.
Add some breakpoint to the version in the add-ons folder,
Switch to the debug perspective,
Start Eclipses debug server,
Start blender
Run the script and it will hit the breakpoint.
The common problems I find people encounter:
pointing the path to the wrong pydev debug folder, ensure that there is a /pysrc folder
When Pydev updates, update the PYDEV_SOURCE_DIR as the debug_xxxxx will have change
not having eclipse server running,
setting breakpoints on a local copy of the files instead of the version in the blender add-on directory
saving the script does not mean that blender will reload it, use imp, disable/renable the add-on or restart Blender.
There are quite comprehensive instructions for setting up blender and eclipse for debugging.
http://wiki.blender.org/index.php/User:Z0r/PyDevAndProfiling
While this is for blenders game engine, much of it applies to regular blender.