How to Import the Following Function in Another File

How do I call a function from another .py file?

First, import function from file.py:

from file import function

Later, call the function using:

function(a, b)

Note that file is one of Python's core modules, so I suggest you change the filename of file.py to something else.

Note that if you're trying to import functions from a.py to a file called b.py, you will need to make sure that a.py and b.py are in the same directory.

Importing a function from another file, where to import other libraries?

In Python each file is a module. Each module has its own namespace - its own set of variables. Each function also has its own local namespace.

When you use the name pd in a function defined in the module func, it will first look for the local variable pd in the function - if it doesn't exist,
it will look for it in the namespace of its module, func. It will not look for it in the module main, even if the code calling the function is in main.py

This is known as lexical scoping - the rule is that variables are looked up close to where the code is defined, not where it is used. Some languages do look up variables close to where the code is used, it's known as dynamic scoping- in one of these languages something like your solution #3 would work, but most languages including Python follow lexical scoping rules, so it won't work.

So pandas has to be imported in funcs.py. main.py doesn't have to import or even
know anything about pandas to use make_df.

If you import pandas at the top of func.py, then when you import the module func from main.py, the line import pandas as pd at the top of func.py will be executed, the pandas module will be loaded, and a reference to it will be created in func bound to the name pd. There is no need to re-import it in main.py.

If you do re-import pandas in main.py, Python will be smart enough not to reload the entire module just because you imported it in two places, it will just give you a reference to the already loaded pandas module.

Putting the import in the body of the function will work but it's not considered good practice, unless you have a really good reason to do so. Normally imports go at the top of the file where they are used.

importing class and its function from another file

At the bottom of your index file you create a HtmlTemplate object and call all the methods on it. Since this code is not contained in any other block, it gets executed when you import the module. You either need to remove it or check to see if the file is being run from the command line.

if __name__ == "__main__":
objx=HtmlTemplate()
objx.Header()
objx.Body()
objx.Form()
objx.Footer()
objx.CloseHtml()

Cannot import function from another file

I was able to solve the issue, it was related to some imports I was making in my file, when I removed all the import statement in my hotel_helper.py ,the code started working as expected , Still I don't understand reason why the issue was occurring. anyway it works.

Importing function from another .py file imports more than just that function?

I was under the impression that importing a function would only import that function.

It seems there's an incorrect assumption about what a from-import actually does.

The first time a module is imported, an import statement will execute the entire module, including print calls made at the global scope (docs). This is true regardless of whether the mymodule was first imported by using a statement like import mymodule or by using a statement like from mymodule import myfunction.

Subsequent imports of the same module will re-use an existing module cached in sys.modules, which may be how you arrived at the misunderstanding that the entire module is not executed.

There is a common pattern to avoid global level code being executed by a module import. Often you will find code which is not intended to be executed at import time located inside a conditional, like this:

def myfunction():
for number in range(0,10):
print(number)

if __name__ == "__main__":
print("Hi")


Related Topics



Leave a reply



Submit