Pergunta

I have a python script file that works perfectly when I use it from the terminal.

Now I have created the following .desktop file in order to launch it easily:

[Desktop Entry]
Name=Test
GenericName=Test
Comment=My test script
Type=Application
Exec=/opt/test.py
Icon=/opt/test.png
Categories=Utils;

When I launch it the GTK window appear but clicking a button that call an init.d script make it working not properly.

Therefore adding Terminal=true make it working perfectly but I don't want to have that terminal open.

So I have then put the following code in order to log the environment variables:

import os

with open("/tmp/py_env.log", "w") as env_log:
  env_log.write(str(os.environ))

and found differences.

So my question is how to write the .desktop file so that my application is running like if I start it from my terminal (without having an opened terminal :))

Foi útil?

Solução 3

Thanks anyone to have participate to this question.

I have solved this issue by implemented use of pkexec instead of gksudo. pkexec seems to reuse the current user environment then I don't have this issue anymore.

Thanks.

Outras dicas

The problem is valid, but I think "replicating the terminal environment" is the wrong approach to solve it.

Indeed, what makes the application work is not the fact that it's launched from the terminal, it's that the terminal happens to have some environment variables which matter to your application.

Therefore, what you should aim for is to have those environment variables set properly at all times, rather than assuming the terminal environment will always happen to contain them all the time for all your users.

Thus, you should:

  1. Check which environment variables are different between the two environments
  2. Make a list of those which matter (i.e. those which would make the .desktop file work properly), and of what their value needs to be for the script to work
  3. Either:
    • Create a wrapper script for your Python script, which initializes those environment variables properly, OR
    • Set those environment variables from inside the Python script itself.

this question is similar to .bashrc not read when shell script is invoked from desktop shortcut

  • either initialize your environment in ~/.bash_profile instead of ~/.bashrc

OR

  • make your *.desktop file call a wrapper that initializes your environment - e.g. by sourcing ~/.bashrc (or whatever script is responsible now).

the second solution is more specific (does not effect all other unrelated launches of your shell) in should thus be preferred.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top