First, I have read many threads on this - so I understand it looks like a dupe. But, let me clarify why I still felt the need to post this question - most answers are at least 4 years old and I am hoping that python has introduced better ways to do this. Here goes. I have a python project with the the following structure:
project1/__main__.py
constants.py
|
|__folder1/mod1.py
__init__.py
I am trying to run main.py from the project1 dir (this code in turn calls a function in mod1.py). In mod1.py, I am trying to import constants.py using (and I tried all the below options)
#1 from constants import * [results in error - NameError: name 'constants' is not defined]
#2 from ..constants import * [results in error - ValueError: attempted relative import beyond top-level package]
#3 from project1.constants import * [results in error = ModuleNotFoundError: No module named 'projec1']
As you can see, none of the above options are working. I would rather not use the sys option recommended in many of the threads. Is there any other clean way to do this? What am I missing?
You should have misspelled something.
from constants import *
and
import constants
should work for the structure you've provided.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.