Home > Software engineering >  Python on RPi: Errno 2 No such file or directory: 'config.json' but config.json is in the
Python on RPi: Errno 2 No such file or directory: 'config.json' but config.json is in the

Time:10-18

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related