I need to import only a single function from another python file which runs stuff in it, but when I import the function, it runs the entire code instead of importing just the function I want. Is there anyway to only import a single function from another .py file without running the entire code?
If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for modules in the given module: >>> import os, sys >>> file_path = 'AdditionalModules/' >>> sys.
This happens because when Python imports a module, it runs all the code in that module. After running the module it takes whatever variables were defined in that module, and it puts them on the module object, which in our case is salutations .
Using import works great on Python modules and packages, but import will not work on non-Python data files, such as text files (including JSON, HTML, csv, etc.) or binary files (such as images).
So there's four different ways to import: Import the whole module using its original name: pycon import random. Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd.
In another.py
, move the code that you don't want to be ran into a block that only runs 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 the another
module and the my_func
function will not run at import.
from another import my_func # Importing won't run the function.
my_func(...) # You can run the function by explicitly calling it.
You could move the function in question to another file and import it into your file.
But the fact that you are running everything on import makes me think you need to move most of the stuff in your imported module into functions and call those only as need with a main guard.
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()
rather than what you probably have, which I guess looks like
print "one"
print "two"
def what_i_really_want_import():
print "this is what I wanted"
With the main guard anything in a function will not be executed at import time, though you can still call it if you need to. If name == "main" really means "am I running this script from the command line?" On an import, the if conditional will return false so your print_one(), print_two() calls will not take place.
There are some good reasons to leave things in a script to execute on import. Some of them are constants, initialization/configuration steps that you want to take place 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, don't leave too much code to execute on load, otherwise you'll end up with exactly these problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With