2

I have a rather unusual problem. I wrote a python script which needs API keys. Since I don't want them floating around on the internet I created a seperate .json with the keys and added it to .gitignore. So far so good.

I wrote the program with VSCode and there I could execute it no problem. But when I try to use my program with the normal PowerShell it simply won't work. I get this error message when I run it on the external PS: FileNotFoundError: [Errno 2] No such file or directory: './master-folder/key.json'

I use a virtualenv, for the packages but that shouldn't affect anything (of course I activated it in PS). Here's the part of the code once again:

keys_fp = './master-folder/key.json'

keys = load(open(keys_fp, 'r'))

The folder structure is as follows:

.
├── programs
│   └── program.py
└── key.json
nerdlab
  • 65
  • 1
  • 6

1 Answers1

3

Based on your comments, your working directory is

E:\Git\master-folder\programs\

and inside your script, you're referencing

./master-folder/key.json

which resolves to

E:\Git\master-folder\programs\master-folder\key.json

but that doesn't exist. If you adjust your script to use the proper path, it should resolve your problem:

keys_fp = f'{os.getcwd()}\\..\\key.json'

According to this answer, you can access the script's root with the following:

import os
root = os.path.dirname(os.path.realpath(__file__))
keys_fp = f'{root}\\..\\key.json'
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63