Resolve environment variables in run configuration

Answered

Hi,

I have a PyCharm run configuration for a program. I holds a bunch of arguments. Some arguments are environment variables like:

'$foo' or '${bar}'

I get flakey behaviour when I run code. Sometimes:

'$foo' is resolved to 'my_variable_1' and '${bar}' is resolved to 'my_variable_2' etc.

But sometimes they just stay as they are. I.e. the arguments are taken to be literally '$foo' or '${bar}' and so my code crashes.

My work around is to restart PyCharm. But it's getting annoying. 

How can I get it to work more consistently?

Cheers,

Graham

8 comments
Comment actions Permalink

Hi Graham,

My first guess was that your environment variables are not loaded to PyCharm, and hence, Python doesn't get them, but it strange that it works differently from time to time.

Could you please, for the sake of the experiment, start PyCharm from the system terminal and see how it works then?

Has it helped?

0
Comment actions Permalink

Hi Sergey,

 

Thanks for the quick response! I was wrong before:

The true behaviour is that environment variables get resolved when I run a configuration via 'debug' but the never get resolved when I run its with 'run'.

I stared PyCharm with the command line with:

/Applications/PyCharm.app/Contents/MacOS/pycharm

And, once in I get the same behaviour: 'debug' results in 'contents_of_env_var' but 'run' results in '${env_var}'

...At least I can just run in debug with no breakpoints, so I guess it's not the end of the world.


Cheers,

Graham

0
Comment actions Permalink

Interesting...
Where do you set those environment variables?

Could you please also share a screenshot of your Run/Debug configuration?

0
Comment actions Permalink

Environment variables are set in my .bash_profile.

 

Run config is:

 

Where the contents of 'Parameters:' are essentially:

--db-username

${USERNAME}

--db-password

${USERPASS}

--data-directory

"${HOME}/Desktop/foo"

--dataset-name

foo

run

 

 

0
Comment actions Permalink

I can't reproduce it on my side.

Are there any steps for reproducing that you noticed?
Please add the following debug log setting to Help | Debug Log Settings..., reproduce the problem, collect logs via Help | Show Log in... and file an issue to our issue tracker using the link https://youtrack.jetbrains.com/issues/PY

#com.intellij.execution.configurations.GeneralCommandLine

0
Comment actions Permalink

Sergey Karpov I have a similar issue, which seems configuration dependent. When I execute a simple Python test run configuration, my system environmental variables are present, and my env parameters expand. For example this simple test prints my local $http_proxy twice.


import os
import sys


def test_pycharm():
print(os.environ['http_proxy'])
print(sys.argv)


if __name__ == '__main__':
test_pycharm()

However, if I try to use variable expansion when executing a Docker run configuration the environmental variables are not expanded


FROM baseimage
ARG https_proxy
ENV http_proxy=${http_proxy}
RUN echo ${http_proxy}

Connected.
appuser@2aad36f7254d:~$ env
SU_EXEC=/usr/sbin/gosu
HOSTNAME=2aad36f7254d
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
JAVA_VERSION=1.8.0_275
PWD=/app
HOME=/app
http_proxy=${http_proxy}

You can see the literal value ${http_proxy}.

I am not sure where to report this behavior but I found this thread and thought it may be relevant or at least you could point me in the right direction.

0
Comment actions Permalink

Hi Mr Waffles,

I'm not sure it's supposed to work in Build args. We probably do not parse it that way and just pass it as a text.
It may be worth filing a feature request to https://youtrack.jetbrains.com/issues/IDEA
If I'm wrong and there is a way to make it work, then guys from the IDEA team will let you know.

1

Please sign in to leave a comment.