I am trying to run two python scripts on startup on my Raspberry Pi.
Both scripts work if I launch them with
python script1.py
python script2.py
They also work with a shell script in the same folder containing
python script1.py &
python script2.py &
But if I add either the individual scripts to the rc.local startup file, or the shell script, I get errors of missing files.
If I add the python scripts to startup via rc.local then it claims that text files I call in the script are missing:
pi#raspberrypi ~ $ sudo service rc.local start
My IP address is 192.168.0.4
pi#raspberrypi ~ $ Traceback (most recent call last):
File "home/pi/scripts/script1.py", line 8, in <module>
scripts.make_batch(randint(10,12))
File "/home/pi/scripts/functions.py", line 36, in make_batch
num_lines = sum(1 for line in open('script1_file.txt'))
IOError: [Errno 2] No such file or directory: 'script1_file.txt'
Traceback (most recent call last):
File "home/pi/scripts/script2.py", line 5, in <module>
scripts.check(3,30)
File "/home/pi/scripts/functions.py", line 78, in check
with open('script2_file.txt', 'r+') as followed:
IOError: [Errno 2] No such file or directory: 'script2_file.txt'
^C
If I add the batch script.sh to startup that calls the python scripts then it claims they are missing.
pi#raspberrypi /etc $ sudo service rc.local start
My IP address is 192.168.0.4
pi#raspberrypi /etc $ python: can't open file 'script1.py': [Errno 2] No such file or directory
python: can't open file 'script2.py': [Errno 2] No such file or directory
^C
Is this something to do with permissions? My script folder is 755 recursive.
All the files ARE there and the scripts run fine directly. This is something to do with running files within files through the rc.local startup process... but I don't know what!
Help greatly appreciated.
You need to supply the full paths to the files:
with open('full/path/to/script2_file.txt', 'r+')
The same for your second problem:
'full/path/to/script2.py'
Related
Hey all
I am trying to run a python script from GitHub, https://github.com/dudisgit/gmod_toolgun_prop for a project with a functioning screen and I put a command at the end of the .bashrc file
python3 /home/pi/gmod_toolgun_prop-main/main.py
so that the code executes as soon as the RPi powers up. When running the script in Thonny's Python IDE on my RPi 2B it executes no problem and the screen works. However when I open terminal I get an error message from the code running in the .bashrc file:
Traceback (most recent call last):
File "/home/pi/gmod_toolgun_prop-main/main.py", line 381, in <module>
main()
File "/home/pi/gmod_toolgun_prop-main/main.py", line 357, in main
with open(args.config) as config_file:
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
However, the config.json file is in the same folder as the main.py file as shown below:
Screenshot of file explorer showing config.json is in the same folder as main.py
And here's a screenshot of the error message
And here's the code that is refenced in the error message as line 357:
with open(args.config) as config_file:
config = json.load(config_file)
The entirety of the main.py script is in the Github link as attached in the first paragraph as well as the config.json file and other relevant files.
I am fairly new to the Python programming space so I don't understand what could be causing this error nor how this script handles opening the config.json file.
I have tried creating a custom service but it spits out the same error. Crontab and the local.bashrc file just doesn't work straight up for this. This is the furthest I have got with it attempting to execute on boot.
Maybe it helps to use the absolute path of config.json, i.e.(according to your description)
/home/pi/gmod_toolgun_prop-main/config.json
instead of the simple filename config.json.
If Python says the file is not there, then the file is not there. The only question is, where is there?
The filename in this case is config.json. Since there's no / (no directory name), the name is taken to be relative to the current working directory. That might or might not be the same as the directory of the main Python module, here /home/pi/gmod_toolgun_prop-main/main.py.
You can verify that by printing the current working directory just before opening the file. You can use os.getcwd to do that. Or, use strace(1) to show the interpreter's attempts to open config.json.
When I execute python helloworld.py in powershell. The response is this C:\Users\ragal\AppData\Local\Microsoft\WindowsApps\python.exe: can't open file 'C:\Users\ragal\helloworld.py': [Errno 2] No such file or directory.
What should I do
I am trying to run a python script that downloads qualtrics survey responses and stores it as a CSV. As part of the script it calls zipfile.zipfile. When i run the program inside of VS Code Terminal window it downloads and unzips the file just fine. However when I run the script in Windows Task Scheduler i run into this error.
<class 'PermissionError'>
Traceback (most recent call last):
File "C:\Users\sdr-research\Dropbox (PCC)\SDR6\Qualtrics_Data\QualtricsSurveyResponses\mindshareResponsesPull.py", line 54, in <module>
zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall("C:\\Users\sdr-research\Dropbox (PCC)\SDR6\Qualtrics_Data\QualtricsSurveyResponses")
File "C:\Python36\lib\zipfile.py", line 1501, in extractall
self._extract_member(zipinfo, path, pwd)
File "C:\Python36\lib\zipfile.py", line 1555, in _extract_member
open(targetpath, "wb") as target:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\sdr-research\\Dropbox (PCC)\\SDR6\\Qualtrics_Data\\QualtricsSurveyResponses\\Mindshare English v2.1.csv'
I am running python 3.6, windows 10, i have tried changing the user permissions from my logged in account the administrator's, placing the file's path in as the program to run, placing python.exe as the program to run and the file's path as the first argument with and without the folder the file is located in as the "start in" parameter. I make sure when i put in the file name into the task scheduler i put quotations around the path because there is a space in the path.
Any ideas?
I had a similar issue with a python script not being able to create a log file when opened with task scheduler (when I scheduled Selenium to do a daily browser task). It was solved by checking "Run with the highest privileges" in the General tab of the task.
I've followed the instructions to build a Donkey Car 2 and I'm trying to start the web server using docker.
When I try to run bash start-server.sh I get the following error:
start-server: Running Donkey server container...
Using TensorFlow backend.
Loading modules for server.
hello
Traceback (most recent call last):
File "/donkey/scripts/serve.py", line 12, in <module>
w = dk.remotes.DonkeyPilotApplication()
File "/donkey/donkey/remotes.py", line 175, in __init__
self.pilots = ph.default_pilots()
File "/donkey/donkey/pilots.py", line 84, in default_pilots
pilot_list = self.pilots_from_models()
File "/donkey/donkey/pilots.py", line 71, in pilots_from_models
models_list = [f for f in os.scandir(self.models_path)]
FileNotFoundError: [Errno 2] No such file or directory: '/root/mydonkey/models'
How do I get this to work?
A simple change to the start-server.sh script fixed the issue for me.
Change line 4:
mkdir -p ~/mydonkey
to this:
mkdir -p ~/mydonkey/models
I figured this out.
Run bash start-server -d (with the -d flag) to open the docker with a terminal prompt.
Inside the docker terminal, run python scripts/setup.py to create the necessary folders in the ~/mydonkey folder.
Now you can start the server by running python scripts/serve.py inside the docker terminal.
After you've collected some images you'll see them inside and outside docker in the ~/mydonkey folder.
I'm have a problem where if I double click my script (.py), or open it with IDLE, it will compile and run correct. However, if I try to run the script in my windows command line, using
C:\> "C:\Software_Dev\Python 2.7.1\python.exe" C:\path\to\script\script.py
I get...
Traceback (most recent call last):
File "C:\path\to\script\script.py", line 66, in <module>
a.CheckTorrent()
File "C:\path\to\script\script.py", line 33, in script
self.WriteLog(fileName)
File "C:\path\to\script\script.py", line 54, in WriteLog
myFile = open(r'%s' %(filename), 'w')
IOError: [Errno 13] Permission denied: './TorrentMonitor.log'
So my question is, why am I getting permission errors when I run this script through command line in window 7 but not when I double click? What's the difference between those two processes?
Thanks in advance!
The script is trying to write into a file in the current directory. In the example above, you're starting it from C:\ where you probably don't have write permissions.
cd to a directory that you own, and you should be able to run that command just fine.
This is because when you double-click the file (or when running it from IDLE), the current working directory is the directory that contains your script. When starting it from the command line, it's C:\ which you don't seem to have write access to.