I'm having trouble importing a function:
def helloworld():
print("Hello World!")
I have it in a file called hello.py inside the folder: C:/projects.
I enter the following:
>>> import sys
>>> sys.path.append('C:/proyectos')
>>> import hello
>>> helloworld()
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
helloworld()
NameError: name 'helloworld' is not defined
I don't understand why it gives me this error if I have defined the helloworld function inside the hello.py file. Does anyone know how to import a function that you have created inside a .py file?
Thank you very much for your help
As you import the module, the namespace is
hello
, so the correct access is:You could also import all module references into the main program's namespace with:
or, better yet, import only the function that interests you