pedroprates
pedroprates

Reputation: 79

How to import a folder with "-" character on python?

I am having trouble importing a folder with the - character on Python 3.9. The structure is as follows:

folder/
   __init__.py
   utils_a.py
folder-utils/
   __init__.py
   utils_b.py
main.py

I need to import folder-utils/utils_b.py on main.py. The following code works for folder.utils_a but throws a ModuleNotFoundError on folder-utils.utils_b:

from folder.utils_a import hello_world_from_a
from folder-utils.utils_b import hello_world_from_b

I have also tried to replace the - with a _, and did not work either:

from folder.utils_a import hello_world_from_a
from folder_utils.utils_b import hello_world_from_b

I did not want to import with importlib or any other package, but I am failing to see how to declare the folder with -.

Upvotes: 0

Views: 1623

Answers (1)

crock
crock

Reputation: 461

The - character is reserved in Python for arithmetic, and cannot be used in any variable, module, or library names. You'll have to rename any folder using a - character.

The folder name is invalid, its impossible to import the folder while it contains that character. You can't import an invalid folder/package with or without importlib.

Upvotes: 1

Related Questions