0

How would I import config.py from the current working directory? Why does import ..config return the following error: ImportError: attempted relative import with no known parent package?

main_directory
|
|
|--sub_directory
|     |
|     |
|     current_working_directory
|
|
config.py
martineau
  • 119,623
  • 25
  • 170
  • 301
Lucas Schwartz
  • 174
  • 1
  • 13
  • `import` has no knowledge of directories. It only cares about _packages_. If your code isn't structured as a Python package, relative imports don't apply. If you want to access `config.py` as an independent module with any encompassing package, your only option would be to add `main_directory` to your Python path and then use `import config`. – Brian61354270 Jun 03 '21 at 15:39
  • I see. So the next obvious step is to learn about python packages I guess. Thank you for your response. – Lucas Schwartz Jun 03 '21 at 15:45
  • Fortunately, the [official documentation](https://docs.python.org/3/tutorial/modules.html) is pretty stellar. You may also find these questions helpful: [What's the difference between a Python module and a Python package?](/questions/7948494/) and [Execution of Python code with -m option or not](/questions/22241420/) – Brian61354270 Jun 03 '21 at 15:49
  • Thanks for recommending the tutorial. It has really helped to understand relative imports in python. Keep up the good work! – Lucas Schwartz Jun 13 '21 at 22:53

0 Answers0