I have a complex python program I'd like to debug where the setup.py has
entry_points=dict(
console_scripts=[
'myprog = myprog.command:myprog_main',
]
)
where the command.py has the logic to accept command so I can run something like
myprog process --config config.yaml
Placing a breakingpoint in pycharm doesn't cause the program to stop, since doing python command.py process --config config.yaml doesn't do anything
I feel this something basic, but I couldn't find a way to debug this (using pycharm)
Let's take jupyter notebook as an example:
In jupyter, it from jupyter_core.command import main, so what I need to do is placing a breakpoint in jupyter_core.command:main.
And then, I need to add a configuration in Pycharm. Script path should be /path/to/jupyter, Parameters should be notebook.
Next, I need to click Debug.
I've done, I reach the breakpoint in jupyter_core.command:main.
In case this answer doesn't work for you, try this:
add a run configuration in PyCharm
configure it with Module name instead of Script path.
Set the Module name to myprog.command
Add the following section to myprog/command.py:
if __name__ == "__main__":
myprog_main()
Sadly, setting the Module name to myprog.command:myprog_main doesn't work.
Set the Working directory to the root of your repo (i.e. the parent of the module myprog such that the imports work).
Note: I used the exact names from OP's question. Please adjust the names of functions, modules, and packages for your problem accordingly.
Related
I want to run pylint on all my modules, which are in different locations in a big directory. Because running pylint in this directory is still not supported, I assume I need to walk through each module in my own Python scripts and run pylint on each module from there.
To run pylint inside a Python script, the documentation seems clear:
It is also possible to call Pylint from another Python program, thanks
to the Run() function in the pylint.lint module (assuming Pylint
options are stored in a list of strings pylint_options) as:
import pylint.lint
pylint_opts = ['--version']
pylint.lint.Run(pylint_opts)
However, I cannot get this to run successfully on actual files. What is the correct syntax? Even if I copy-paste the arguments that worked on the command-line, using an absolute file path, the call fails:
import pylint.lint
pylint_opts = ["--load-plugins=pylint.extensions.docparams /home/user/me/mypath/myfile.py"]
pylint.lint.Run(pylint_opts)
The output is the default fallback dialogue of the command-line tool, with my script's name instead of pylint:
No config file found, using default configuration
Usage: myscript.py [options] module_or_package
Check that a module satisfied a coding standard (and more !).
myscript.py --help`
[...]
What am I missing?
I know that epylint exists as an alternative, and I can get that to run, but it is extremely inconvenient that it overrides the --msg-format and --reports parameters and I want to figure out what I am doing wrong.
The answer is to separate the options into a list, as shown in this related question:
pylint_opts = ["--load-plugins=pylint.extensions.docparams", "/home/user/me/mypath/myfile.py"]
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'm trying to write a gimp script using pythonfu. However, when I try to run script locally, I get an error
`--> ./vvv.py
Traceback (most recent call last):
File "./vvv.py", line 5, in <module>
from gimpfu import *
ImportError: No module named gimpfu
I gather that the script might be only loadable through gimp. However, the script doesn't appear at gimp menus. In that case, how do I get the error output?
depending on your os, you have to look for GIMP plugin's directory, something like:
GIMP 2\lib\gimp\2.0\plug-ins
and copy your script there. Of Course you have to add registration stuff something like this:
register(
"python-fu-draw", #<- this is plugin name
N_("brief"), #<- description
"Long", #<- description
"name surname", #<- author
"#Copyright 2013", #<- copyright info
"2013/10/29", #<- creation date
N_("_Draw..."), #<- label shown in gimp's menu
"", #<- kind of image requested from your script (INDEX,RGB,...and so on)
[ #<- input parameters array
(PF_FILE, "ifile", N_("Color input file"), 'default.txt'),
],
[], #<- output parameters array (usually empty)
draw, #<- main method to call
menu="<Image>/Filters/", #<- Where add your plugin
domain=("gimp20-python", gimp.locale_directory)
)
main() #<- don't forget this
Once you copied the script into right directory with right registration stuff you can run GIMP and run your script selecting it in drop down menu you selected in registration stuff.
You don't have to run it from a python console. So those wouldn't work:
python myscript.py
./myscript.py
>>> myscript
To debug your script interactively open a python-fu console from gimp:
Filters->Python-fu->Console
Take a look at this web site. Mainly slides are very useful.
While if you want to run your script in a batch mode please take a look at this
This is expected behavior. GIMP's Python plugin has an unusual design: DO NOT call the Python script directly but rather call GIMP which calls your script in turn (this is true both of GUI and command line scripts).
Make sure you register your central function at the end of the script:
from gimpfu import *
# ... your script ...
args = [(PF_STRING, 'file', 'GlobPattern', '*.*')] # adjust to your function
register('my-script', '', '', '', '', '', '', '', args, [], my_function)
main() # GIMP's main(), do not write your own!
Mark your script executable
chmod +x my_script.py
Copy, move or hardlink your script so that it's in GIMP's plugin directory:
ln my_script.py ~/.gimp-2.8/plug-ins/my_script.py
You can now call GIMP which will call your script (warning: GIMP is a bit slow)
gimp -i -b '(my-script RUN-NONINTERACTIVE "example.png")' -b '(gimp-quit 0)'
As discussed above, DO NOT do this:
python my_script.py # bad!
The parentheses are due to it being a Scheme function call. For that same reason you have to replace all hyphens in the list of available functions with underscores (can be found in the GIMP GUI under Help > Procedure Browser).
If you need an example script, check out the documentation or my script.
A common cause of Python plugins silently not appearing in GIMP is syntax errors. A quick, scriptable way to check:
python -m py_compile vvv.py
On Windows platforms, a successful compile will return an %errorlevel% of 0.
Paraphrasing the original post: I wrote a Python GIMP plugin and installed it in the proper place, but it doesn't appear in GIMP's Filter menu, so how do I debug it?
The most common cause is that you didn't give execute permission on the plugin's source (.py) file when you copied it to the proper place. (It's a known quirk of GIMP that it won't read Python plugin source that doesn't have execute permission.)
Another question might be: my plugin appears in the GIMP Filter menu, but it silently doesn't work as I expect, how do I debug it?
You can either start GIMP in a console (a terminal) so that any debugging print from your plugin goes to the console, or use Filters>Python-Fu>Console as suggested in another answer.
If your plugin crashes, the GIMP GUI will show you the traceback, or it will appear in a console you started GIMP in.
Another question might be: can I debug Python scripts that import PyGimp modules? (most commonly gimpfu, which is an omnibus module that imports other PyGimp modules.) For example, you have a Python module for use by a GIMP Python plugin, and you want to test portions of it (say, doctest it) without starting GIMP.
You might be able to install PyGimp (into the Python directories) by the standard: download PyGimp, .configure, make, make install. (I haven't tried that and could not easily find a downloadable source package for PyGimp.) Usually, the PyGimp modules are installed with GIMP, and not installed in the Python directories (GIMP just futzes with PYTHON_PATH when it starts a Python interpreter so that the PyGimp modules are found?) Or in your environment (of the shell or your IDE) you could change PYTHON_PATH so that it includes the subdirectory of your GIMP installation that holds the PyGimp modules.
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.
I'm attempting to run python on a system that doesn't allow me to set environment variables. Is there a commandline flag to python that will set PYTHONHOME? I looked here: http://docs.python.org/release/2.3.5/inst/search-path.html but didn't see anything.
So, hopefully something like this:
python -magical_path_flag /my/python/install test.py
EDIT
Thanks for the responses everyone. I'm embarrassed to say I actually meant PYTHONHOME, not PYTHONPATH. (That's what I deserve for asking a question at 1:30 AM.) I've edited my quesiton.
Here's some more info. I'm trying to get python running on Android. I can run python -V no problem, but if I try and execute a script, I get:
I/ControlActivity(18340): Could not find platform independent libraries <prefix>
I/ControlActivity(18340): Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Unfortunately when using the ProcessBuilder and changing the environment variables on Android, it says that they're not modifiable and throws an exception. I'm able to pass all the command line flags I want, so I was hoping I could set PYTHONHOME that way.
I've tried creating a wrapping shell script which exports PYTHONHOME and then calls python but that didn't work. (Got the same error as before.)
Thanks,
Gabe
You could simply set it in your script -- sys.path is a regular, modifiable list. Something like:
import sys
sys.path.append("/path/to/libraries")
should do the trick
In UNIXy shells, you can set an environment variable just for the duration of one command by prepending the command with the environment variable setting:
$ PYTHONPATH=/my/python/install python test.py
If none of the other answers suit you, you can write a wrapper script that temporarily sets the environment variable then exec's your other script. For example:
python mywrapper.py -magical_path_flag /my/python/install test.py