Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a python module without running it

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?

like image 978
Me Man Avatar asked Jun 24 '15 16:06

Me Man


People also ask

How do I use a Python module without installing it?

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.

Why is Python running a module when I import it?

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 .

Can you import non Python files?

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).

What are the three ways to import modules in Python?

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.


2 Answers

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.
like image 183
bakkal Avatar answered Oct 01 '22 01:10

bakkal


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.

like image 37
JL Peyret Avatar answered Oct 01 '22 02:10

JL Peyret