PyCharm remote debugging doesn't respect PYTHONPATH settings

I'm having an issue with debugging a remote script. When starting the debugger, the debugger stops with an ImportError, because it can't find the modules imported by the script. Here's the traceback shown in the debugger:

ssh://n7@dmai:/opt/itinerator/pyre/bin/python -u /home/n7/.pycharm/pycharm_helpers/pydev/pydevd.py --client '0.0.0.0' --port 41016 --file /home/n7/dm/src/lib/datamap/classify.py
pydev debugger: starting
Connected to pydev debugger (build 117.663)
Traceback (most recent call last):
  File "/home/n7/.pycharm/pycharm_helpers/pydev/pydevd.py", line 1401, in <module>
    debugger.run(setup['file'], None, None)
  File "/home/n7/.pycharm/pycharm_helpers/pydev/pydevd.py", line 1055, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "/home/n7/dm/src/lib/datamap/classify.py", line 12, in <module>
    from common.pyutils import log_warn, log_debug
ImportError: No module named common.pyutils
prettyPrint();
Checking sys.path while the script is running shows that it indeed does not have the correct paths loaded. My profile on the remote server has a custom .bashrc that augments the PYTHONPATH with the correct paths - Pycharm obviously doesn't respect that. Shouldn't be a problem, though, because the Configuration settings for the remote interpreter that I set up have an Environment Variables section... so I just added the correct PYTHONPATH variable there. However, this is apparently being ignored, because it has no effect.

This problem means I can't debug remote scripts. What am I missing? How do I get the remote script to run with the correct PYTHONPATH?
10 comments
Comment actions Permalink
Unfortunately we found out that there is no portable way to pass environment variables over an SSH connection, so we are unable to apply the PYTHONPATH settings in the run configuration. You need to configure your remote environment in such a way that it doesn't rely on an externally defined PYTHONPATH environment variable.
0
Comment actions Permalink
Are you serious? Supporting PYTHONPATH is one of the most basic things an environment needs to have, because of the way the interpreter imports other modules. This is very unfortunate. Remote debugging is the prime reason my company paid for a corporate license. I will be reviewing this decision with my team and we may need a refund if there is no workaround for this.
1
Comment actions Permalink
Absolutely. The Python ecosystem provides a wide array of tools (virtualenv, pip, buildout etc.) that allow you to package your application in a way that declares dependencies on other modules explicitly, and does not rely on a globally defined PYTHONPATH.

The easiest workaround that you can use without repackaging your application is to create a shell script that defines the PYTHONPATH environment variable and then runs the actual Python interpreter, and to specify that script as the remote interpreter that PyCharm runs.

In case you will need a refund on your purchase, please contact sales@jetbrains.com.
0
Comment actions Permalink
Hmm, that is not a bad idea at all. I'll try out the shell script workaround. And yes, we are looking into using virtualenv to control our python environments, but we aren't quite there yet... Thanks for the tips.
0
Comment actions Permalink

I tried to use the trick with bash script. It works, when I run python script at remote machine, BUT it doesn't work, when I try to do debugging of a python code. Debug attempt is terminated with following error:

Error running remote_machine
Can't run remote python interpreter: Couldn't obtain remote socket from output , stderr   File "<string>", line 1
import
^
SyntaxError: invalid syntax

Content of the bash script:

#!/bin/bash

# Set system variable to run subscription manager
export PYTHONPATH=/path/to/module/:/path/to/another/module/
export SOME_SYS_VARIABLE=3

# Run python with arguments
/usr/bin/python $@

I have to say that the ability to do remote debugging and setting of PYTHONPATH is cruical for me!

Please, offer some reliable solution!

1
Comment actions Permalink

I have to also note that setting PYTHONPATH (using export PYTHONPATH) in .bashrc or .bash_profile doesn't work too. It seems that /home/user/.pycharm_helpers/pydev/pydev_run_in_console.py ignores $PYTHONPATH :-(

1
Comment actions Permalink

I found evil hack. I added required paths to sys.path in starting script /home/user/.pycharm_helpers/pydev/pydev_run_in_console.py and /home/user/.pycharm_helpers/pydev/pydevd.py.

0
Comment actions Permalink

Jhindek, I have the same problem can you please elaborate on your solution on how to remotely debug a script that is missing a module?

0
Comment actions Permalink

Adding PYTHONPATH as an additional environment variable in the run/debug settings worked for me and a remote interpreter.

3
Comment actions Permalink

Hanan Shteingart Solution worked for me as well. Thanks.

0

Please sign in to leave a comment.