0

import function from a file in the same folder

I got a pretty good answer in this post on how to import a file from a sub directory. But now I also want to know how to import a subdirectory that contains a space, so from the original example in the post above; lets say that we have an identical sub directory called "app 2" and I want to import the config.py from that sub directory.

DoubleDibz  
├── app 2
│   ├── __init__.py
│   ├── api 
│   │   ├── __init__.py
│   │   └── helloworld.py
│   ├── app.py
│   ├── common
│   │   ├── __init__.py
│   │   └── constants.py
│   ├── config.py
│   ├── extensions.py
│   ├── static
│   └── templates
└── run.py

How would I go about doing this?

import app 2.config doesn't seem to work and adding single or double quotes does not seem to work either.

chessset5
  • 11
  • 1

1 Answers1

0

As seen in the top answer of the question shared by @truth, you can do something like this:

_config = __import__("app 2.config")

Ideally a Python project would not have folder names with spaces in them, as this is method is not recommended, so if this is yours then I would suggest to just change the name of that folder to app_2

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19