Pycharm SSH remote interpreter: editor doesn't recognize module - python

I'm currently trying to get PyCharm running with a remote interpreter via SSH.
The connection itself is working well, however one of my imported modules "ROOT" is not recognized by the editor ("No module named ROOT" at the "import ROOT" statement).
Before "ROOT" can be imported in any python session (while using the Terminal and not PyCharm), a "thisroot.sh" file with environment variables etc. has to be called. Thus, it's sourced at every ssh login with aid of the .bash_profile/.bashrc file.
Therefore, if the .sh file is not sourced before starting python in a SSH terminal, python cannot find the module.
Hence, I think the problem is that PyCharm doesn't call the .bashrc file (and thus doesn't source the "thisroot.sh" file) after establishing a connection. If I open a remote Python console directly in PyCharm, the "ROOT" module is not found as well.
For a less complicated module, one could add the environment variables by hand into the PyCharm settings. For my module however, it's not really feasible.
Is there any way such that PyCharm recognizes the module in the editor (and finds it in the remote Python console)? I've tried sourcing the "thisroot" file with the PyCharm StartUp script settings, but it didn't work till now.

Related

Load Environment Variables from shell script before python task | Pycharm 2018.1 PRO

I'm trying to run a python task within run/debug configuration task option.
My problem is that I need to load some environment variables from a bash script.
In my normal terminal is as simple as <source file.sh> and then I can execute my script without any problem.
I already tried doing a external tool (to execute the sh file) an put on "before launch" option inside run/debug configuration
but did not work.
Any ideas/help?
You could set the environment variables before starting PyCharm. This way, they should propagate to processes launched by PyCharm, and you c an re-use the existing bash script.
But if you can put the environment variable settings into a file of the appropriate format, the EnvFile suggestion in this answer appears to be a good solution.

Pycharm cannot find libraries from remote virtualenv, even when correct interpreter specified

I want to use Pycharm as IDE to upload my python code to a remote server. On this server I have a virtual environment with virtualenv. This interpreter is specified in the project interpreter settings and when I look at the installed packages, it finds all the packages, but when I program, all the imports I use are not found and hence autocomplete does not work and my whole code is full of errors which is quite annoying. I use Pycharm on a windows computer and the server runs on Ubuntu 14.04.
As can be seen on the figure, the project interpreter uses an ssh connection to the server, and my path to the python interpreter is set to the python interpreter of the virtual environment (not the default python on that server). All the installed packages are also shown. Should I configure something else to make Pycharm find these modules (allthough I think pycharm finds them as they show up in the picture, but it does not use them for imports)?
I had a similar problem, but not with a remote interpreter.
I found that the Run Configuration was set to use a different interpreter than the Project Settings.
Settings / Preferences > Project: untitled > Project Interpreter =>
~/project/venv/bin/python
All good. But:
Run Configuration > Python interpreter => /usr/local/bin/python3.7
The top choice on the drop-down list for the Run Configuration interpreters was Project Default which set it to the same as my project setting.
Run Configuration > Python interpreter => Project Default
(~/project/venv/bin/python)
I don't know if this is the cause of your problem, but it's something to check.
As discussed in PY-29551 PyCharm does not yet support remote virtual environments with remote interpreters (only global Python environments). Apparently they will add this any moment in PY-6002.
Until then I solved this with the workaround suggested in PY-29551:
For simply deploying and running code the workaround with adding a "pseudo" python executable that just loads the venv before running python worked for me:
#!/bin/sh
. /home/me/mypy/venv/bin/activate
exec python $*
Just add this to your remote venv and select it as Python executable instead.
For remote debugging this did not work for me, so for debugging I instead went for the Remote Debug Server approach described in the offical PyCharm docs on remote debugging.

Cannot Import MySQLdb in PyCharm, runs fine in terminal.

I have problems trying to run a script which imports MySQLdb within PyCharm.
Running the script from terminal works just fine while running within PyCharm fails with
ImportError: No module named MySQLdb
I have tried this thread and it helped making things work in the terminal.
Trying to set the environment variables in the IDE though does not seem to work.
In PyCharm Run Config I set the environment variables
DYLD_LIBRARY_PATH - /usr/local/mysql/lib/libmysqlclient.18.dylib
PATH - /usr/local/mysql/lib/
but I still get the ImportError.
As #Dilettant pointed me to https://stackoverflow.com/a/34992894/1989141,
I realised there are two different places I was supposed to set the Python interpreter in PyCharm.
The first is in the main preferences (as in phil’s solution in the link) and allows me to specify a path to a local folder for mysqldb module.
The second is in the edit configuration settings for the script I want to run. I noticed the interpreters were different.
The script was set to run with the Python 2.7.6 /System/Library/Frameworks/...... version (which I believe is the OSx pre-installed version).
In the terminal and in PyCharm general settings I am using Python 2.7.9 in /usr/local/bin/python.
Matching the script interpreter to the one in the main settings (for which I manyally added mysqldb folder) solves the ImportError. Also I removed the environment variables I set up in the Edit Configuration as they are not needed.
Hope this helps. Thanks to both #Dilettant and #julivico for their suggestions.

Debugging project with root in PyDev/LiClipse

For a project I'm doing, which uses scapy and therefore sockets, I need to be able to debug as root.
I have already figured out how to start the interpreter as root without the system asking for permission. I added:
user ALL=(root) NOPASSWD:/home/user/git/Project/bin/python2.7
to /etc/sudoers.tmp. The path I used leads to the python interpreter of the virtual environment which I'm using for this project. The LiClipse project is also using this path. Now I only need to make LiClipse run the interpreter as root, but I don't want to start LiClipse as root. I just want it to use this interpreter as root when debugging. How can I do that?
In this case, I suggest using remote debugging. You can then have the process running as whichever user it needs to, and the IDE can run independently as another user, or even on another server.
Assuming you're using PyDev in LiClipse, you can configure remote debugging by following the documentation for remote debugging.
The basic premise is that you add the pydev debugger library to your Python path and include the following where you need a breakpoint:
import pydevd
pydevd.settrace('localhost', port=7788, stdoutToServer=True, stderrToServer=True)
Then you configure your IDE with a pydev instance listening at that server (7788) in this case.
If you want to use breakpoints from the IDE, you can add the argument suspend=False, and the debugger will not suspend until it encounters your breakpoints.
In PyDev you can actually select a shell script which in turn executes the actual Python to do the run... then, you can configure your shell script to first do any special setup.
I.e.: your script could be a file named python_on_root.sh with contents such as:
#!/bin/bash
source setup_env.bash
sudo python "$#"
Then, in the interpreter configuration, select the python_on_root.sh to create an interpreter that'll execute as root.
Note that the same could be done for other interesting things -- such as running in a docker container or activating a conda environment first -- sky is the limit ;)

gVim ImportErrror: module not found, even though module is in same folder as script

I am using gVim with Windows 7.
I am trying to run a python script using the pyfile % command, but every time I do so, I get ImportError: No module named libtcodpy
Checking the location of the libtcodpy, it is indeed in the same folder as the script I am trying to run. Furthermore, running the program with the python IDE works fine.
What am I doing wrong?
Vim does not automatically search the current script's directory for imports, only some configured ones (cp. :help python-_get_paths), and the current working directory.
So, you either need to configure the current script's path, or simply :cd %:h to it (alternatively automatically via :set autochdir).

Categories

Resources