I am running the python interpreter from the same directory where the imported file my_module.py
resides. When I try to use the function defined in the imported file using only the function name print_hi
, I get a NameError
error message. But it works fine if I reference the function using my_module.print_hi
Is there a way to make this work without referencing the module file name?
Is there a way to make this work without using the from
keyword in the import statement?
PS C:\Users\joe\Documents\Python\General\importing> type my_module.py
def print_hi(name):
print(f'Hi, {name}')
PS C:\Users\joe\Documents\Python\General\importing> python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_module
>>> print_hi('joe')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'print_hi' is not defined
>>> my_module.print_hi('joe')
Hi, joe
>>>