Ok, so when I run this command:
sudo python3.5 /path/to/my/script.py
It executes fine and returns:
select user:
Guest
My username
Choose the account that needs cleaning up (enter corresponding number):
After which I enter "2", press enter and the script runs great.
However, when I run:
osascript -e 'do shell script "python3.5 /Path/to/my/script.py" with administrator privileges'
I get the expected pop-up screen in which to enter my sudo-password, but afterwards it throws out the error:
OSError: [Errno 9] Bad file descriptor (1)needs cleaning up (enter
corresponding number):")
Anyone has any idea on this? Any help would be greatly appreciated!
PS; some background as to why I need this method --> I am using a pre-script (also written in python) from which myscript.py needs to be executed. My prescript.py does not need administrator privileges. Sometimes however, it will be required to call myscript.py which does need administrative privileges. Since I do not want to hard-code my sudo-password into any of my scripts I want to get a pop-up box asking me for it. I figured, telling my pre-script.py to run subprocess.Popen([]) on the osascript command I mentioned above would do the trick. However, for that, I first need to get the osascript command to work properly.
Ah, so I figured this one out myself. As expected it had to do with privileges. Somehow "do shell script" is run via other privileges then just entering the command in terminal. And "sudo command" in terminal does not equal (fully) to "do shell script [script] with administrator privileges".
For me, what fixed it, was navigating to:
System Preferences --> Security and Privacy --> Privacy tab --> Accessibility
And then dragging Python.app and Terminal.app to that window (you might need to unlock it first by pressing on the little lock in the lower-left corner of that window).
Related
i want to run a python script on dietPI. I chose dietPI because i want to boot fast.
When i run a simple tkinter program i get the error _tkinter.TclError: no display name and no $DISPLAY environment variable
Ive already tried setting a display variable like so: export DISPLAY=:0
(i tried other numbers aswell)
but then i get _tkinter.TclError: couldn't connect to display ":0.0"
Any help?
This took me all day to get working. (I also posted this same answer on a different SE site here: https://raspberrypi.stackexchange.com/a/118928/60683)
Note: This is for raspbian, with main user account using the default username ("pi")
Here are the steps in order:
Name your python file main.py and drag your python file to the desktop.
Next, create a new plain text file and add the following contents:
#!/bin/bash
python3 /home/pi/Desktop/main.py
Save this text file as launch.command on the desktop. This is shell script to launch your python file. By default it won't work, we have to chmod it (next step)
Run terminal and run the following command:
chmod u+x /home/pi/Desktop/launch.command
Now double clicking launch.command (and selecting execute) will launch your python file.
At this point, we are ready to get it launching on boot. If you've done any other launch attempts prior to this please undo all of those file changes you had done.
Open terminal and run the following:
sudo nano /etc/systemd/system/myproject.service
We are just going to call it myproject for now, don't change any of this until after you get it working, then feel free to attempt a rename
In the window that pops up, copy paste the following exactly:
[Unit]
Description=Start Myproject
After=graphical.target
Wants=graphical.target
[Service]
User=pi
Group=pi
ExecStart=/bin/bash -c "export DISPLAY=:0; export XAUTHORITY=/home/pi/.Xauthority; /home/pi/Desktop/launch.command"
[Install]
WantedBy=graphical.target
Now press ctrl+x to exit-and-save, it will prompt you to save changes, type "y", the filename should already be entered as .../myproject.service, if it is then press enter and it will save and exit, if the file name is blank it messed up (this just happens sometimes), quit terminal and try all of this again, sorry.
Now in terminal enter:
sudo systemctl daemon-reload
Then
sudo systemctl enable myproject.service
Lastly
sudo reboot
On launch, before anything else shows (after the boot up splash screen shows of course, but before desktop shows) your GUI will now launch, congrats!
I recommend you launch it full screen, to do that edit your python file to use this:
root.attributes('-fullscreen', True)
NOTE, 'root' is likely not what you called your TK() init.... at the end of your code you have something like XXXX.mainloop(), change 'root' to whatever XXXX is, stick this right after your init of TK()
Once you've reboot, you are likely stuck in the app (especially if you launched it full screen), things like alt+f4 or alt+f11 won't kill your program. To exit, press the windows/menu button on your keyboard, the access panel will appear you and can go to accessories>terminal to launch a new terminal window, then run the following:
sudo systemctl stop myproject.service
The program will be killed and you will be back to your standard desktop...
NOW, to prevent it from launching again on bootup, simply rename your launch.command file to DISABLED_launch.command and when you're ready to start launching on bootup again set the name back.
Go get a drink, you've earned it.
Also, you can obviously put these places other than desktop but MAKE SURE you always use the full path in any of these commands, don't use the '~/..' shortcut.
I have made a Python program that uses output.to_csv('results.csv'). When I run it in Spyder it creates that CSV file. But when I double click on it nothing happens. Cmd appears and closes but nothing on the folder, no results.csv.
What am I missing? What more do I have to do?
Run the program from the command line itself instead of double-clicking the .py file.
I assume you are on Windows since you mention CMD. First, cd into the directory containing your program. Then, run python <program>.py or python3 <program>.py depending on your installation.
This time, you will see any output or error messages that appear in CMD without it immediately closing.
If the .csv file really exists, you should be able to go to your File Explorer and find the file at the top of the "Quick Access" section. Right-click the file and hover over "Open With >". Then select Notepad and a notepad will open up showing your results.
If you do not see the file, then try running your program on the command prompt (for Windows):
Press the windows key and type "cmd" in the search bar.
Choose "Command Prompt"
Go to the dir of your program using the cd command
Type python <program name>.py
If there are no errors, follow the steps in the first paragraph.
Ok i guess windows is not recommended at all for this type of tasks. I mean running something simple as create such file is like trying to kill the Lernaean Hydra.
What i did is i just runned it with anaconda prompt and it worked sweet! Thanks for help. Thanks to all!
PS: I'm seriously considering changing to Linux after this
For anyone having the same problem, but have anaconda installed. 1) Open Anaconda Prompt, 2) use cd (1 space) then adress of the folder which contains your py program (eg. cd C:\Users\Bernie\Desktop\tasos) and hit enter, 3) on the next line that appears type: python program_name.py, 4)Hit enter, 5)success!
I use Adobe products (such as Premiere Pro/Photoshop) a lot and wanted to have Discord Rich Presence implementation.
I found a very useful python script for this: https://github.com/smokes/adobe-rpc
If I manually execute it (opens command prompt) it works perfectly fine, it detects my Discord and Premiere Pro/Photoshop and displays it correctly in Discord.
However I wanted to run this script when I launch Premiere Pro/Photoshop so I used a batch file for this:
#echo off
start "Premiere Pro" "C:\Program Files\Adobe\Adobe Premiere Pro CC 2019\Adobe Premiere Pro.exe"
ping -n 20 localhost
python "E:\Software\Python\Adobe Discord Rich Presence\adobe-rpc-master\rpc.py"
It opens the script as it should but when it actually launches the script, the script can not find Discord or the Adobe software.
It just states "Run Adobe/Discord App".
I have tested the script by opening a fresh Command Prompt and dragging the script in: same result as stated above. It can not find Discord or the Adobe software.
This is very strange to me as I can perfectly run it if I open it manually but not if I run it in the Command Prompt?
Furthermore, Python does not give an actual error message, just the "error message" that is built inside the script.
When I launch it manually, it opens in the Command Prompt so I am very confused as to why this happens.
Can anyone explain this/help me?
I am very new to the Command Prompt and Python.
Warning! This is comment and maybe answer both!
First of all: do you have Python 3.4>= added in PATH? If not add.
Second: Do you followed instructions in README.md? If not follow!
Third: Which Adoble application do you use and is it available in adobe-rpc's repository? If not then wait for next update of adobe-rpc.
Fourth: Try to open script in it's directory and this is how:
Go to script's folder
Hold SHIFT and click RMB, also called Right Button Mouse. In menu Open Command Prompt here or Open Powershell here must be displayed.
If Open Command Prompt here is displayed instead of Open Powershell here, click it.
Now do: python scriptname.py ~ I don't know that script usage and if it requires arguments, just do python scriptname.py arguments.
If Open Powershell here is displayed instead of Open Command Prompt here. Go back, but first copy directory.
Then press WIN+R and in Run input type cmd.
In Command Prompt(CMD) type cd and paste your directory, then hit Enter.
Now do same as in 4. line.
Maybe this would help.
Otherwise I don't know.
I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome
I am trying to make either a .bat or .exe file to execute a python script in linux (Raspbian). I would want to have an icon on the desktop which will be clicked (touch screen) and will then execute the python script.
The python script would need 'sudo' authorization... so if I wanted to run in terminal:
sudo python filelocation/name.py
Thanks!
I've never used Raspbian myself, but I guess you can follow the standard procedure to add a custom icon to your desktop:
http://xmodulo.com/create-desktop-shortcut-launcher-linux.html
Use a .desktop file as nodakai already mentioned.
To have the python file executed with root permissions, either
use something like gksudo python program.py. This will ask the user for the password in a normal window, no terminal involved.
if the user shall not be asked for a password, consider an entry in the sudoers file, and use the usual sudo python program.py. (If you use this, make sure your program doesn't allow the user to do whatever they want, but only does the specific tasks you want the user to be able to do as root.)
name.py
#!/bin/python
.....
then set the permissions (all the following commands are done in the terminal)
$ chmod a+x name.py
then you can run it as
$ ./name.py
if you wanted to run it as sudo you would do
$ sudo ./name.py