user12372145
user12372145

Reputation:

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?

Upvotes: 0

Views: 260

Answers (2)

python_user
python_user

Reputation: 186

You can try this 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 *

Upvotes: 2

Ahmad Moussa
Ahmad Moussa

Reputation: 864

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 *

Upvotes: 0

Related Questions