1

i'm trying to connecting Pydrive to my Python Project but I got this

Traceback (most recent call last):
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
    with open(filename, 'r') as fp:
         ^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 386, in LoadClientConfigFile
    client_type, client_info = clientsecrets.loadfile(client_config_file)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2client\clientsecrets.py", line 124, in _loadfile
    raise InvalidClientSecretsError('Error opening file', exc.filename,
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\LENOVO\OneDrive\Desktop\Computational Neuroscience\Data\Data Science.py", line 4, in <module>
    gauth.LocalWebserverAuth(client_secrets='client_secrets.json')
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 113, in _decorated
    self.GetFlow()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 443, in GetFlow
    self.LoadClientConfig()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 366, in LoadClientConfig
    self.LoadClientConfigFile()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 388, in LoadClientConfigFile
    raise InvalidConfigError('Invalid client secrets file %s' % error)
pydrive.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

And That is my code

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth(client_secrets='client_secrets.json')

It says that the file isn't in a directory yet but I've already put it in a directory

enter image description here

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Shi0oo
  • 11
  • 1

1 Answers1

0

It depends on where you run the python executable from. If I make a directory with two files in it:

  • my_dir
    • hello.txt
    • world.py

Where the two files look like: hello.txt

hello
world

world.py

with open("hello.txt") as fh:
    print(fh.read())

If I run world.py like:

cd my_dir
python world.py
hello
world

It reads the file just fine. But if I run it out of that directory like this:

cd ~
python my_dir/world.py
FileNotFoundError: No such file or directory 'hello.txt'

Because the working directory is $HOME.

To fix this, ensure you are using the correct relative path. You can use the __file__ attribute for this:

# world.py
from pathlib import Path

my_dir = Path(__file__).parent

with open(my_dir / 'hello.txt') as fh:
    print(fh.read())

Now it doesn't matter where I'm running my code from

C.Nivs
  • 12,353
  • 2
  • 19
  • 44