Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import Another Python 3 File in Django Settings.py?

I'm using Python 3. I have two Python files in the same directory: first.py and second.py. In the beginning of first.py, I use:

from second import *

However, it returns the following error message:

ModuleNotFoundError: No module named 'second'

How should I import it in first.py?

Update: To clarify my specific use-case, I am trying to split my settings.py in Django. I have a main settings.py file and another one that only includes the confidential information. I followed this following documentation that uses the following line in settings.py:

from settings_local import *

Note that settings_local.py is in the same directory. However, it returns the following error message:

ModuleNotFoundError: No module named 'settings_local'

I know the document says "Some of the examples listed below need to be modified for compatibility with Django 1.4 and later." but I do not know how to use it in Python 3.

like image 220
1man Avatar asked Dec 18 '22 04:12

1man


2 Answers

I just found the solution:

from .settings_local import *

instead of:

from settings_local import *

I found the solution in this thread.

like image 148
1man Avatar answered Dec 20 '22 17:12

1man


You can use the following code snippet:

from .settings_local import *

This is relative import. More Info here and here

like image 33
dalonlobo Avatar answered Dec 20 '22 17:12

dalonlobo