Desktop Launcher for Python Script Starts Program in Wrong Path

2024/10/3 4:38:27

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.

The problem is that the script will be launched in the wrong path - namely the home folder instead of the directory it is placed in. Thereby it can not find other vital files accompanying it in its folder and hence does not work.

To examine that misbehaviour I created a short script to check the folder a python script is executing in:

#!/usr/bin/env pythonimport subprocess
import timesubprocess.call(["pwd"], shell=True)
time.sleep(7)  # to get a chance to read the output

Executing it from its own folder gives the output:

/home/myusername/PythonProjects

I am setting a desktop launcher via Nemo's menu. Now executing the same script yields:

/home/myusername

I do not understand this behaviour. How could I create a working desktop launcher for my python script?

Answer

The page describes the format of .desktop files.

You may note the "Path" element, which specifies the working directory for the file to be run in. In your case you would want a desktop file that specified

Path=/home/myusername/PythonProjects

If Nemo doesn't allow you to set the Path element you may need to edit the desktop file by hand. The .desktop files are text files and you can probably find them in /home/myusername/.local/share/applications/

https://en.xdnf.cn/q/70768.html

Related Q&A

How does numpy broadcasting perform faster?

In the following question, https://stackoverflow.com/a/40056135/5714445Numpys broadcasting provides a solution thats almost 6x faster than using np.setdiff1d() paired with np.view(). How does it manage…

python check if utf-8 string is uppercase

I am having trouble with .isupper() when I have a utf-8 encoded string. I have a lot of text files I am converting to xml. While the text is very variable the format is static. words in all caps should…

Failed building wheel for Twisted in Windows 10 python 3

Im trying to install rasa-core on my windows 10 machine.When installing with pip install, I get: Failed building wheel for TwistedThe same error appears when trying to install Twisted separately.How co…

How to set a fill color between two vertical lines

Using matplotlib, we can "trivially" fill the area between two vertical lines using fill_between() as in the example: https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/fill_between_…

Pythonic way to read file line by line?

Whats the Pythonic way to go about reading files line by line of the two methods below?with open(file, r) as f:for line in f:print lineorwith open(file, r) as f:for line in f.readlines():print lineOr …

Python Selenium clicking next button until the end

This is a follow up question from my first question, and I am trying to scrape a website and have Selenium click on next (until it cant be clicked) and collect the results as well.This is the html tag …

How can i detect one word with speech recognition in Python

I know how to detect speech with Python but this question is more specific: How can I make Python listening for only one word and then returns True if Python could recognize the word.I know, I could ju…

Finding matching and nonmatching items in lists

Im pretty new to Python and am getting a little confused as to what you can and cant do with lists. I have two lists that I want to compare and return matching and nonmatching elements in a binary form…

How to obtain better results using NLTK pos tag

I am just learning nltk using Python. I tried doing pos_tag on various sentences. But the results obtained are not accurate. How can I improvise the results ?broke = NN flimsy = NN crap = NNAlso I am …

Pandas apply on rolling with multi-column output

I am working on a code that would apply a rolling window to a function that would return multiple columns. Input: Pandas Series Expected output: 3-column DataFrame def fun1(series, ):# Some calculation…