How to run python script as executable and keep access to files surrounding it?

Konrad

I have a python program "Alfred.py" that I want to be able to run by clicking an icon or typing "Alfred" in the terminal. It is connected to a database file and pulls in Excel files when asked to. When I try to run it by clicking, this is what I get:

mcaay:~ mcaay$ /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.desktop ; exit;
/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.desktop: line 1: [Desktop: command not found
Traceback (most recent call last):
  File "/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.py", line 59, in <module>
    AND Usterka IS NOT NULL;""")
sqlite3.OperationalError: no such table: repairs
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

This line:

sqlite3.OperationalError: no such table: repairs

means that Alfred.py didn't find .db file, created it but it was empty so there was no table called "repairs". This .db file sits in the same directory as Alfred.py.

What I did until now:

  • added "#!/usr/bin/env python3" as the 1st line of Alfred.py
  • typed "chmod +x /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.py" in terminal to make it executable
  • added "export PATH=$PATH:/Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/" to my .bash_profile to make Alfred.py available from any place in terminal
  • created Alfred.desktop file for a clickable icon:
[Desktop Entry]
Name=Alfred
Exec=python3 /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.py
Terminal=true
Type=Application
  • made Alfred.desktop executable by typing "chmod +x /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.desktop" in the terminal

When I type in terminal "Alfred.py", I get:

mcaay:~ mcaay$ Alfred.py
Traceback (most recent call last):
  File "/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.py", line 59, in <module>
    AND Usterka IS NOT NULL;""")
sqlite3.OperationalError: no such table: repairs

So the same as with clicking "Alfred.desktop". If I type just "Alfred", I get:

mcaay:~ mcaay$ Alfred
-bash: Alfred: command not found

I want just typing "Alfred" to work and I want it to see the database and other files correctly, or alternatively if clicking the icon will work properly it is acceptable too. My Googe-Fu failed me here. What do?

P.S. I noticed that a database file is created in my home directory ("/Users/mcaay"), as if Alfred.py was pulled from original destination to some temporary destination and executed there (other files were not pulled so program crashed).

Konrad

Ok, so after a good night of sleep the things I've read finally computed in my head and I got an answer. Everything I did before is unnecessary and can be discarded.

My solution (keep in mind I'm using macOS Mojave atm):

  1. In home directory, I created a file called .start_alfred.sh. This is a shell script that will have a callable command in terminal to go to my specified directory and then run a python script.
  2. Inside, I wrote this:
#!/bin/bash

function Alfred() {
    cd /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred
    python3 Alfred.py
}

The name of this function (Alfred) is what will invoke the shell commands included inside. It's basically doing what you would normally do in terminal to start a program.

  1. In my .bash_profile, I added source ~/.start_Alfred.sh at the end, so everytime I open the terminal everything that is in .start_Alfred.sh becomes available immediately.

That's basically it :) Maybe not the cleanest but super easy and good enough for what I need. Tanktalus' and asafpr's answers helped me to come up with this solution.

Use case:

mcaay:~ mcaay$ Alfred

Co jest?
>>> thx
np

mcaay:Alfred mcaay$ 

Thx is the command to end the python script.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make my script run from executable python file on Unix if it uses functions imported from other files? (ModuleNotFoundError)

How to run a python script as a loop for different files?

How to run the python script for many files?

How to run a script python on multiple files

How to run a python executable in background?

python: how to keep the script run after plt.show()

How to compile a python script to an executable?

How to run an arbitrary script or executable from memory?

How can I run executable in shell script?

AOSP - How to have an executable run with root access?

How to use python subprocess to run c++ executable file in another folder with providing arguments, inside a python script?

Run an executable with a Shell Script

How can I make a Python script standalone executable to run without ANY dependency?

How to Run Executable in Python Without Pop Up Console of The Executable?

How can you access files after being compiled to a single python executable?

How to list .so files that are loaded on python script run?

Python - How to run script continuously to look for files in Windows directory

How do i run python script on all files in a directory?

How can I run a Python script without source files on Raspbian?

How to run executable with Python code inside

How to run a Nuitka compiled Python executable

How to make a call to an executable from Python script?

How do I make a python script executable?

How do I make a python script executable?

How to make python script executable on osx?

Can I get access to the code of an already executable Python script?

How do I run a windows executable in linux shell script?

how to run an executable file without EXE extension using CMD script?

How to run an executable file without the EXE extension using a CMD script?