PyCharm remote interpreter and environment variables

Completed

I have VM with chroot. All development is going inside chroot.

for example if you run following (ssh server is running inside chroot)

#> ssh user@hostname "env"

it will return you incomplete list of env variables because environment was not initialized

if you run next command

#> ssh user@hostname "bash -l -c 'env'" 

it will return you completed list of env variables because environment was initialized

so, problem is next :

When you run script through PyCharm remote interpreter it uses command like following:

#> ssh user@hostname "<path_to_python> <script_name>"

but it fails, because environment was not initialized and python can't import module (again, because env is not initialized and PYTHONPATH is incomplete)

Question:

is it possible to configure remote interpreter in PyCharm to execute script like following:

#> ssh user@hostname "bash -l -c '<path_to_python> <script_name>'" 

???

2
11 comments

Looks like I solved problem.

I tried to use "Edit configuration" -> "Before launch" - to run bash to initialize environment, but it doesn't help me

I created bash wrapper around python which looks like

#!/bin/bash -l

/usr/bin/python "$@"

and configured remote interpreter to use it - and it works :-) 

All env variables were initialized :-)

5

Your solution is great! Thanks!

0

Hi Svpfido,

I'm having the same problem with environment variables and I'd like to try your solution.

Under "Edit configuration" -> "Before launch", at the "+" symbol i get a list of options for "Add New Configuration".

What did you configure?

0

Hi, Anders Krogsager

I created shell script file like "mypython" and added following lines to it:

#!/bin/bash -l

/usr/bin/python "$@"

 

In PyCharm I used path to this file as path to remote interpreter and now PyCharm will use "mypython" when it connects to remote host and tries to execute python script.

 

 

 

 

1

So I created a file called mypython.sh and added the lines:

#!/bin/bash -l
/home/paperspace/anaconda3/envs/fastai/bin/python "$@"

I placed the .sh file in my project folder on the remote machine.

Then I created a new remote interpreter in pycharm as:

sftp://<username>@<IPaddress>/home/<pathToProjectFolder>/mypython.sh

However when I try to run a .py script I get:

Permission denied
Process finished with exit code 126

0

Anders Krogsager

Actually I did one more thing - I gave right access to this file. In my case I did following:

chmod 755 <path to mypython>

1

Hi @Svpfido,

Thanks for your solution.

It helps me with one machine. But on the other it does not work out. I tried all my efforts but it just does not, e.g., different version of Python (2, 3).

The only work around is that I directly put what's in ~/.bashrc on the server before the /usr/bin/python "$@" in that file as an interpreter.

Do you come up with any idea of what happened to my second machine? Anyway thanks so much.

 

 

0

Hi, Deartonym

I'm not an expert in Linux world but my solution is pretty simple and just runs Python in initialized Bash session.

Since it is just bash script I think (and as I understood you already did it) you can do whatever you need prior Python launching.

In case when it didn't worked for me I just ran all I need in debug mode and read output to understand what was went wrong.

0

Hi Svpfido,

Thanks for your reply.

I still do not figure out what goes wrong with one of my machine but it does not matter.

This workaround is awesome on our cluster machine.

0

why doesn't pycharm just use the .bashrc in the machine?

 

1

Old question, but I share my foundation in the topic for newcomers. So when you use the ssh interpreter, the pycharm opens an ssh connection to the remote machine. But in this case it is not sure the bash reads your profile, TBH I did not follow the exact logic, but you can read it here and here if you are interested. To be sure it happens I recommend to ssh to the remote machine with the config you gave to the pycharm, and run the env command, then you will see which variables are provided for you in that bash. In my use case the solution was easy, because I wanted to connect to my remote dev docker container. So I added to its entry.sh, that is given at entrypoint in the docker-compose file, the
env | grep _ >> /etc/environment

command to collect and save the env variables which contain _. It is true the most of the variables which are used by applications. And that was enough, after this I reached my env variables from the bash. Tha original idea came from here.

1

Please sign in to leave a comment.