Import python module without starting it

I need to import only one function from another python file that runs something in it, but when I import this function, it runs all the code instead of importing only the function that I want. Is it even possible to import only one function from another .py file without running all the code?

+9
function python module python-import
source share
5 answers

In another.py move the code you donโ€™t want to run to a block that runs only when the script is explicitly called to run, and not just imported

 def my_func(x): return x if __name__ == '__main__': # Put that needs to run here 

Now, if you are in your_script.py , you can import another module, and the my_func function my_func not work when importing.

 from another import my_func # Importing won't run the function. my_func(...) # You can run the function by explicitly calling it. 
+16
source share

You can move the function in question to another file and import it into your file.

But the fact that you use everything related to import makes me think that you need to move most of the material to the imported module in functions and call them only as necessary with the help of the main defender.

 def print_one(): print "one" def print_two(): print "two" def what_i_really_want_import(): print "this is what I wanted" if __name__ == '__main__': print_one() print_two() 

and not what you probably have that looks like

 print "one" print "two" def what_i_really_want_import(): print "this is what I wanted" 

With the main guard, anything in the function will not be executed during import, although you can still call him if you need to. If the name == " main " really means that I am running this script from the command line? "When importing, the if condition returns false, so your calls to print_one (), print_two () will not be executed.

There are several good reasons to leave things in a script to execute upon import. Some of them are constants, initialization / configuration steps that you want to perform automatically. And having a module level variable is an elegant way to achieve a singleton.

 def print_one(): print "one" def print_two(): print "two" time_when_loaded = time.time() class MySingleton(object): pass THE_ANSWER = 42 singleton = MySingleton() 

But by and large, do not leave too much code to execute at boot, otherwise you will get exactly these problems.

+2
source share

In another python script that you are going to import, you have to put all the code that should be run when the script starts inside the next one, if block is

 if '__main__' == __name__: 

Only when this python file is run as a script, the __name__ variable will be __main__ . When you import a script, any code inside this if condition will not work.

+1
source share

The above option is good when I improve a module that I can change by adding "if name ==" main ::. But if I import a module that Iโ€™m not allowed to modify, then what?

0
source share

1. Open in the editor 2. Find the definition 3. Copy the paste into the old-fashioned

The simplest solution is sometimes the dirtiest.

-8
source share

All Articles