skip to Main Content

I am new to ubuntu .I compiled a python script to an executable file in ubuntu 22.04 using pyinstaller (one file). It connects to a database by reading login credentials from a text file in the same directory.
such as ip address, password, port and username.The code is;

def remote_db_connector():
  txtPath = Path.cwd().joinpath("text.txt")
  myfile = open(txtPath)
  x = myfile.read()
  myfile.close()
  return x

I collect the individual components by using split(). It works perfectly from the folder where the executable and text.txt file resides. When I created a desktop shortcut in ubuntu 22.04 it does not read the text file and the program does not run. The desktop short cut script is as follows;

[Desktop Entry]
Type=Application
Name=Mypro
Exce=/home/softwares/Mypro
Icon=/home/softwares/Myico.png
Terminal=true
StartupNotify=false

(I found it out by giving the shortcut script Terminal=true). Please help me.

2

Answers


  1. Spell Exec correctly, maybe add a Path to your desktop file

    [Desktop Entry]
    Type=Application
    Name=Mypro
    Exec=/home/softwares/Mypro
    Path=/home/softwares
    Icon=/home/softwares/Myico.png
    Terminal=true
    StartupNotify=false
    
    Login or Signup to reply.
  2. You didn’t share your .desktop file nor the entire script, but from what I can see, it looks like you’re giving your script wrong directions by using Path.cwd

    Try it yourself.

    Go into /path/to/yourscript.py and run your script as you normally do.

    Then switch to the parent directory and try the same. It will fail.

    pathlib.Path.cwd returns the directory from where the script was invoked, not its location.

    exchange

    txtPath = Path.cwd().joinpath("text.txt")
    

    with

    txtPath = Path(__file__).parent.joinpath("text.txt")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search