简体   繁体   中英

Cannot import my own dependencies in Python

I have created utils.py and constant.py (saved in the same folder of all src file) and I wrote this in main.py

import utils
import constant

but when I try to run the entire program it gives me this error:

Unable to import 'utils'

and if I open utils.py I noticed this error:

Unable to import 'constant'

(because I need constant also in utils.py)

How can I solve it?

You can try this from . import utils from . import utils and have a look at this Relative imports for the billionth time

try this one:

import sys

sys.path.append("<path/to/project>")

import utils

Or

from .utils import *

The correct way of importing functions from another file is done like this:

from file_name import function_name

If you want to import everything you should do this:

from file_name import *

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM