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.
Related
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
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).
I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you
I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.
So I have a python game (PyGame) running on a raspberry Pi.
I have followed the instructions found on many sites for getting the Raspberry Pi to auto login (those all work), auto run startx, but I'm stuck on getting my program to run once the GUI loads.
Many people (here on StackOverflow and other places) point to this presentation here:
http://www.slideshare.net/SeggySegaran/raspberry-pi-autostarting-a-python-program
I've tried both ways of doing it (putting the desktop file in autostart or putting the command in rc.local
I have opened up a Terminal window and copy / pasted the command to verify there are no typos and the code will run......
sudo python /home/pi/valley.py
and it will run. Is there a way to see a log to find out WHY the program doesn't launch? Is there a better way to get done what I want to get done?
I've got my python script to run at startup doing this:
sudo nano /etc/xdg/lxsession/LXDE/autostart
This will allow you to add an element to run when the LXDE desktop session begins (the raspian default GUI if setup to do from raspi-config)
It will probably have entries like these:
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
It's just a matter of adding your script there as well
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
#python /home/pi/yourAwesomePyScriptHere.py
If your python script uses GPIO, you need to run that as root (using sudo):
#sudo python /home/pi/yourGPIOScript.py
One thing I do want to point out: always test your script before hand.
I mean, run with the absolute path, make sure it still works, try to break it, make sure it's as robust as it can be. If there are errors in your script and you place it at start up you won't see those in a terminal window, but you will hog the cpu with python stuck in a loop at startup.
Also check out this answer on the RPi exchange
You can achieve this in two ways:
1). Using LXDE autostart.
2). As a service via init.d.
If you are starting X with "startx", you can also just stick your game in your .xinitrc. If your game binary is called "game" and is in your path, just do this:
echo "game" >> ~/.xinitrc
This works for other commands. Add a "&" if you want the command to keep running in the background.
This is how I start my window manager, load my wallpaper, start a compositor, etc. It is stupid simple, easy to change later, and can do anything you can do at a terminal prompt.
You can run your script automatically on startup of raspberry by using crontab.
Crontab is table that list all command to perform on scheduled time.
First, you need to edit crontab by using:
sudo crontab -e
And after this, add following line:
#reboot python path-of-your-script & (& should be at the end of line that means command will execute in background).
Save your script and reboot your system. When your system will start, your script will run automatically.