Generally when you create files to be imported you must use if __name__ == '__main__' which evaluates to false in case you are importing the file from another file. So your hi.py may look as:
def me(string):
print(string)
if __name__ == '__main__':
# Do some local work which should not be reflected while importing this file to another module.
me('hello')
Answer from ZdaR on Stack OverflowHow to call a python file with arguments from another python file?
python - How do I call a function from another .py file? - Stack Overflow
Call function with argument in another Python file - Stack Overflow
Call function inside function in another file
This is where arguments in functions are used.
If you want your create_table to be able to be used with different configs, then it needs to accept the config function as an argument.
def create_table(config_func):
config_func(config_name)
...Or the config itself as dictionary.
def create_table(config):
table = config['table']
delimiter = config['delimiter']
create_table({'table': True, 'delimiter': ";"}) More on reddit.com How do I call a function defined in another Python file?
Why do I get ModuleNotFoundError when importing my own file?
What does importlib.import_module do?
I have a file that collects data on a website given a URL. Using command line, I call call this file as:
python data_collector.py url
Now, I would like to capture this same functionality, but in another python script. I know I can import the file as a module and use the functions, but is it possible (or even useful) to simply call it in another file as if I were using command line and return the results? If so, is this an efficient manner of doing things?
First, import function from file.py:
from file import function
Later, call the function using:
function(a, b)
Note that file is one of Python's core modules, so I suggest you change the filename of file.py to something else.
Note that if you're trying to import functions from a.py to a file called b.py, you will need to make sure that a.py and b.py are in the same directory.
Do not write .py when importing.
Let file_a.py contain some functions inside it:
def f():
return 1
def g():
return 2
To import these functions into file_z.py, do this:
from file_a import f, g
Hi
I have two function in functions.py file:
def config(name):
if name == "presentation":
table = True
delimiter = ";"
def create_table():
...
name = client.get_bucket()
config(name)
...
It works. But What I need is only create_table in a functions.py file, and call it in multiple python files with their own config(name) file1.py... file2.py.
I tried this: from functions.create_table import create_table
but the function depends only of config() function inside functions.py
This is where arguments in functions are used.
If you want your create_table to be able to be used with different configs, then it needs to accept the config function as an argument.
def create_table(config_func):
config_func(config_name)
...
Or the config itself as dictionary.
def create_table(config):
table = config['table']
delimiter = config['delimiter']
create_table({'table': True, 'delimiter': ";"})
You could make config an argument of your create_tables() function. And you can use the config function from the same file where create_tables is defined as default.
Like this:
def config(name):
# do some stuff
def create_tables(cfg = config):
...
cfg(name)
# Use with default config:
create_tables()
In your other files, you can then define another config function and use that by passing it as argument to create_tables()
from functions import create_tables
def another_config(name):
# alternative configuration
# Use with your new configuration:
create_tables(cfg = another_config)
Important: don't execute the function while you pass it as argument. Just treat it like any other variable.
Say I am making a project and the main program imports a module B which contains functions to extract data from a document file.
Then, the functions in module B will call functions C, D or E depending on the type of file (docx, pdf, txt). The code for these functions is quite long and quite different, so I have written a seperate .py script for each, and imported these into module B
my question is this:
From C, D or E, I might want to call another function F.
Where should I put function F? In module B? In another .py module? Do I need to import it into C, D and E? I'm guessing I can't as that would be a circular import?
thanks for advice .
I'd like to have my python file call the __main__ from another file. However, the other __main__ expects command-line arguments... how can I call it and wait for until it returns/exits before continuing my file's execution?
Let us have two files in the same directory. Files are called main.py and another.py.
First write a method in another.py:
def do_something():
return "This is the do something method"
Then call the method from main.py. Here is the main.py:
import another
print another.do_something()
Run main.py and you will get output like this:
This is the do something method
N.B.: The above code is being executed using Python 2.7 in Windows 10.
Specify the module then the file then the import like so:
from this_module.anotherfile import dosomething
or if you want all functions from "anotherfile.py"
from this_module.anotherfile import *
and then you can call the "dosomething" command without the "anotherfile" prefix.