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:
[Desktop Entry]
Name=Alfred
Exec=python3 /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.py
Terminal=true
Type=Application
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).
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):
.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.#!/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.
.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.
Comments