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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Welcome to SO. Please consider including whatever error traceback you get in your post. – ewokx Sep 29 '20 at 09:47
  • The information provided is not enough to give a definitive answer, other than guessing. Please see the [mcve] and [ask] pages how to help us help you. In specific, how do you run your code? – MisterMiyagi Sep 29 '20 at 09:49
  • 4
    Try adding `_init__.py` file in every folder – Ajay A Sep 29 '20 at 09:49

1 Answers1

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.

Ava_Katushka
  • 144
  • 8
  • 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. – MisterMiyagi Sep 29 '20 at 09:58
  • Thanks a lot Ma'am. –  Sep 29 '20 at 21:39