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>'" 

???

10 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 :-)

3

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.

 

 

 

 

0

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>

0

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?

 

0

Please sign in to leave a comment.