0

I have a code in Python-Part/Modules&Packages/salute.py

def say_hello(name): print(f'Hello {name}') fruits = { 'name': 'Grapes', 'color': 'Green' } 

I have a code in Python-Part/Modules&Packages/my_module.py

import salute from salute import fruits salute.say_hello('Rafi') print(fruits['name']) 

When I put them in the Python-Part directory they were working fine but when I move them into Python-Part/Modules&Packages, my_module.py is not importing salute.py.

Now, I want to know why this is happening and how to overcome from it?

3
  • 1
    Welcome to SO. Please consider including whatever error traceback you get in your post. Commented Sep 29, 2020 at 9:47
  • The information provided is not enough to give a definitive answer, other than guessing. Please see the minimal reproducible example and How to Ask pages how to help us help you. In specific, how do you run your code? Commented Sep 29, 2020 at 9:49
  • 4
    Try adding _init__.py file in every folder Commented Sep 29, 2020 at 9:49

1 Answer 1

1

The __init__.py files are required to make Python 2 treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

In the simplest case, __init__.py can just be an empty file.

So, try to add __init__.py to package directories you intend to use.

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

2 Comments

This is not the case in python 3. A directory without __init__.py is a namespace package which, for the purpose of this issue, will behave like a normal package.
Thanks a lot Ma'am.