0

I'm trying to save my passwords to a separate .py file and import them into an API login script.

I just want something simple, have tried (Password file) details.py

username = "MyUser"
password = "Pass123"
appKey = "123Key"

and then imported to my script:

import betfairlightweight
from .details import username, password, appKey

Error message:

ImportError: attempted relative import with no known parent package

trading = betfairlightweight.APIClient(username, password, app_key=appKey)
trading.login_interactive()

Why am I receiving this error and how can I resolve it?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Dallas
  • 35
  • 1
  • 9
  • Using an SQL database would be much easier in the long term. Check out https://docs.python.org/3/library/sqlite3.html – El-Chief Jan 27 '20 at 17:04
  • I just want something quick and simple at the moment @el-banto - This is my first day using Python – Dallas Jan 27 '20 at 17:11

1 Answers1

1

It's a common mistake in Python to confuse the dot in a import path as the dot in the folder system. In Python, the dot refers to the current package, while in the folder system it refers to the current folder. Basically a package in Python is defined as a folder with a __init__.py file.

So here when you enter from .details import username, what Python does is it tries to import username from the module details in the current package. If the current folder has a __init__.py file, no problem, but I don't think it's your case, hence the error.

What you want to write here is : from details import username, password, appKey.

You should see this SO post to understand more about relative imports.

Hope this helps!

bastantoine
  • 582
  • 4
  • 21
  • That is a very clear an informative answer Bastien. You are correct, the folder has `no __init__.py file` I removed the `.` but am now receiving a `ModuleNotFoundError` `No module named 'details'` – Dallas Jan 27 '20 at 17:20
  • Hum... Are you sure the name is right? Like `detail`` instead of `details`? Are you sure `details.py` is in the same folder as your "main" file? – bastantoine Jan 27 '20 at 19:05