how to reload a function from a module in python - python

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)

Related

What can modules provide us that functions can't in python?

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.

Accessing a Module Imported from Another Module's Function

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

Does using 'import module_name' statement in a function cause the module to be reloaded?

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.

Linking Python Files Assistance

I understand how to actually link python files, however, i don't understand how to get variable's from these linked files. I've tried to grab them and I keep getting NameError.
How do I go about doing this? The reason i want to link files is to simply neaten up my script and not make it 10000000000 lines long. Also, in the imported python script, do i have to import everything again? Another question, do i use the self function when using another scripts functions?
ie;
Main Script:
import sys, os
import importedpyfile
Imported Py File
import sys, os
I understand how to actually link python files, however, i don't
understand how to get variable's from these linked files. I've tried
to grab them and I keep getting NameError.
How are you doing that? Post more code. For instance, the following works:
file1.py
#!/usr/bin/env python
from file2 import file2_func, file2_variable
file2_func()
print file2_variable
file2.py:
#!/usr/bin/env python
file2_variable = "I'm a variable!"
def file2_func():
print "Hello World!"
Also, in the imported python script, do i have to import everything
again?
Nope, modules should be imported when the python interpreter reads that file.
Another question, do i use the self function when using another
scripts functions?
Nope, that's usually to access class members. See python self explained.
There is also more than one way to import files. See the other answer for some explanations.
I think what you are trying to ask is how to get access to global vars from on .py file without having to deal with namespaces.
In your main script, replace the call to import importedpyfile to say this instead
from importedpyfile import *
Ideally, you keep the code the way you have it. But instead, just reference those global vars with the importedpyfile namespace.
e.g.
import importedpyfile
importedpyfile.MyFunction() # calls "MyFunction" that is defined in importedpyfile.py
Python modules are not "linked" in the sense of C/C++ linking libraries into an executable. A Python import operation creates a name that refers to the imported module; without this name there is no (direct) way to access another module.

Import a module into a module, can't explain what i'm trying to do

So, the following is an example. Here's a module (called feedy.py) in let's say, the core directory:
import feedparser
feed = feedparser.parse("http://site.com/feed")
[...]
A bad example, but anyway: my problem is in the main script (parent dir), I have to do
import feedparser
as well as
from core import feedy
However, is there a way to eliminate the need to import feedparser, since it's already imported in feedy.py?
Hope you understand,
Fike.
In principle, yes. However, it's not usually a good idea.
When you import a module, you effectively declare a local variable which is a reference to that module. So in feedy you have an object called feedparser, which happens to be the module feedparser, although you could at any time reassign it to be any other Python object.
When you import feedy, you can refer to any of feedy's exported variables as feedy.name. So in this case, feedy.feedparser is the module feedparser.
However, if you change the way feedy is implemented, so that it doesn't import (or export) feedparser, this will break your main script. In general you don't want to export everything you have defined, although it's fine for a quick hack.

Categories