The Issue:
I want to use a module that has been imported from within a different module with it's function.
What I want to achieve:
main.py
import differentFile
print (differentFile.functionName.os.getcwd())
differentFile.py
def functionName():
import os
What I have tried:
Pretty much the above, but it doesn't work as functionName as no function os.
I have managed to achieve the above without using the function, but I need to use the function.
There is no reason to do this, you can simply import os in main
Related
For example I have a module names my_mod.py and the code is:-
def get_sum(int1, int2):
a = int(int1) + int(int2)
print(a)
And I have the same function in the python file. my_python_file.py:-
def get_sum(int1, int2):
a = int(int1) + int(int2)
print(a)
get_sum(12, 43)
So what is the main difference between modules and functions? Not talking about my example code.
Main Question:-
Can anyone give me an example about what can modules do that functions can't?
Thank you!
Modules are libraries of function(s).
A module is a file.
my_mod.py
A function is a code.
my_func()
You import a module.
import my_mod
From a module you can import a function.
from my_mod import my_func
You can pass variables in a function, you will not pass a variable in a module.
from my_mod import my_func
my_func(myvar)
You can install a module (if published here on PIP)
pip install my_mod
You can not install a function. The function is defined in your module.
#my_mod.py
def my_func(myvar):
return myvar
When you are going to use the same code multiple times in the same script, you make it into a function to reduce redundancy right? Similarly if you are going to use the same code in multiple different scripts, instead of writing the same function again and again in all the scripts, we write it into a module and we can import and use the function whenever needed. This is a simplified explanation based on how you have used modules alone.
I think in general the Idea with modules, is that a person would write a module, (collection of code for other people to access). It is perhaps a collection of related code to be used as a tool.
Let's say I have a structure like this:
tests----------------
___init__.py
test_functions_a
functions-----------
___init__.py
functions_a
functions_b
I want to test a function from functions_a, but inside functions_a I am importing a function from functions_b.
When I am trying:
from functions.functions_a import function_aa
I am getting an error, because inside functions_a I have a line:
from functions_b import function_bb
and not:
from functions.functions_b import function_bb
How can I solve this?
Any good practises are welcome, as I have no experience in structuring projects.
According to Google Python Style Guide, you should:
Use import statements for packages and modules only, not for
individual classes or functions. Note that there is an explicit
exemption for imports from the typing module.
You should also:
Import each module using the full pathname location of the module.
If you follow those two conventions, you will probably avoid, in the future, situations like the one you just described.
Now, here's how your code will probably look like if you follow those tips:
Module functions.functions_a:
from functions import functions_b as funcs_b
def function_aa():
print("AA")
def function_aa_bb():
function_aa()
funcs_b.function_bb()
Module functions.functions_b:
def function_bb():
print("BB")
And, finally, test_functions_a.py:
from functions import functions_a as funcs_a
if __name__ == "__main__":
funcs_a.function_aa()
funcs_a.function_aa_bb()
Output:
AA
AA
BB
You cannot directly import the function instead you could import File 1 to some other File and then call the function from that particular file you imported .
I know that when we do 'import module_name', then it gets loaded only once, irrespective of the number of times the code passes through the import statement.
But if we move the import statement into a function, then for each function call does the module get re-loaded? If not, then why is it a good practice to import a module at the top of the file, instead of in function?
Does this behavior change for a multi threaded or multi process app?
It does not get loaded every time.
Proof:
file.py:
print('hello')
file2.py:
def a():
import file
a()
a()
Output:
hello
Then why put it on the top?:
Because writing the imports inside a function will cause calls to that function take longer.
I know that when we do 'import module_name', then it gets loaded only once, irrespective of the number of times the code passes through the import statement.
Right!
But if we move the import statement into a function, then for each function call does the module get re-loaded?
No. But if you want, you can explicitly do it something like this:
import importlib
importlib.reload(target_module)
If not, then why is it a good practice to import a module at the top of the file, instead of in function?
When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.
Even though it does not get reloaded, still it has to check if this module is already imported or not. So, there is some extra work done each time the function is called which is unnecessary.
It doesn't get reloaded after every function call and threading does not change this behavior. Here's how I tested it:
test.py:
print("Loaded")
testing.py:
import _thread
def call():
import test
for i in range(10):
call()
_thread.start_new_thread(call, ())
_thread.start_new_thread(call, ())
OUTPUT:
LOADED
To answer your second question, if you import the module at the top of the file, the module will be imported for all functions within the python file. This saves you from having to import the same module in multiple functions if they use the same module.
I have a function myFunc(a,b) defined in myMod under MyFolder.
I import the function and call the function in the following way, it works.
from MyFolder.myMod import myFunc
myFunc(a,b)
Now I update my function . I would like to reload my function, but reload(myMod.myFunc) does not work. reload(MyFolder.myMod) does not work either. May i know the reason?
Modules are compiled only once when you import them. And when you change them, python won't pick them up unless re imported i.e recompiled. So to get around that you can do this
import importlib
importlib.reload(module_name)
for python 3.x
And for python 2.x you can
reload(module_name)
I'm trying to write a program that is related to another program, so how can I access the individual functions from my previous program and work with them as required, I know you are suppose to import that program but what to do after that.
The program that I import how can I use functions from it.
It would be better if you write code.
I assume your 1st code is code1.py and you want to use fun1 from it in code2.py.
Put them in same directory . . .
Use:
import code1
code1.fun1()
Or
from code1 import *
fun1()
Note: 2nd method is not recommended.
If you want to import it from another folder you should also try to use os.chdir().
But mostly you can use either import * to import all of the functions or just import and then the functions you want to import.