0

I have the following folder structure:

C:
└── dev
    └── my_scripts
        ├── __init__.py
        └── my_utils.py

The file my_utils.py includes some function f.

The following code works:

import sys
sys.path.append('C:\dev')
from my_scripts import my_utils
my_utils.f()

However the following does not work:

import sys
sys.path.append('C:\dev')
import my_scripts
my_scripts.my_utils.f()
> AttributeError: module 'my_scripts' has no attribute 'my_utils'

Why am I getting this error?

1
  • As stated in Python docs, you need to add an import in the __init__.py file (in this case from .my_utils import * would work) in order to be able to do what you want. Commented Dec 11, 2024 at 13:27

1 Answer 1

0

It's simple.

You have to import any module you want to be able to access to it via name of the folder "my_scripts" you just have to import them in "__init__.py" file.

like this: __init__.py:

# this is the __init__.py file
from . import my_utils

We use "from ." so it is referring to "my_scripts" folder just like if we use "from .." it refers to "dev" folder because "." means the current folder and ".." means parent folder.

If you want to know more about python import system: python import system

That's all everything will work fine now.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.