As another answer has noted, this will not work because of how Python scopes variables.

Instead, therefore, what I suggest is that you move all these variables into a separate file, e.g. constants.py:

main.py

from functions import a

print(a())

constants.py

X = 1

functions.py

from constants import X

def a():
    return X

Then, running import main prints 1.

Answer from gmds on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope. The search operation of the import statement is defined as a call to the __import__() function, ...
Discussions

Importing a function
For some reason, I can’t import a function from another file. I’ve tried: # levels.py def level(): return 1 # main.py from levels import * print(level()) But it outputs an error: root@1de6ab8b7b69:~/my-project# /bin/python3 /home/workspace/my-project/main.py Traceback (most recent call ... More on discuss.python.org
🌐 discuss.python.org
7
0
July 22, 2024
Import inside functions
An import statement is not free. Importing modules in a hot path may add some (small) overhead. Imports within functions may delay ImportErrors up until much later and make debugging harder. Dependencies of a module or script are more hidden if the imports are not at the top of the file, and the module may be harder to read and understand. Importing from within threads has some limitations, and it is easy to call functions in threads without knowing that they contain import statements. Other than that, if it makes sense for your use-case, why not? More on reddit.com
🌐 r/Python
17
10
January 5, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-import-function
__import__() function in Python - GeeksforGeeks
July 11, 2025 - __import__() is a built-in function in Python that is used to dynamically import modules. It allows us to import a module using a string name instead of the regular "import" statement.
🌐
GeeksforGeeks
geeksforgeeks.org › python › importing-python-functions-from-other-scripts
Importing Python Functions from Other Scripts - GeeksforGeeks
July 23, 2025 - Modular programming in Python enables reuse and structure of code. To accomplish this, importing functions from other scripts or modules is a potent method. This procedure facilitates collaborative development, promotes concern separation, and improves code readability.
🌐
Dive into Python
diveintopython.org › home › learn python programming › functions in python › ways to import functions
Examples of Ways to Import Functions in Python from Another Files
May 3, 2024 - For example, let's say you want to import the runsqlscript function from the sqlideutils module. Here's how you could do it using the from keyword: from sqlideutils import runsqlscript · And here's how you could do it using the import keyword: import sqlideutils with runsqlscript = sqlideutils.runsqlscript · Organizing your Python code is crucial to make it readable, maintainable, and reusable.
🌐
GeeksforGeeks
geeksforgeeks.org › python › import-module-python
Import module in Python - GeeksforGeeks
April 25, 2026 - Python modules are of two types: built-in (come with Python) module and external modules like pandas and numpy, which need to be installed separately. Built-in modules can be directly imported using "import" keyword without any installtion. This ...
🌐
Medium
wbarillon.medium.com › the-easiest-way-to-import-your-functions-or-parameters-in-python-2f0a1a71084a
The easiest way to import your functions or parameters in python
November 1, 2020 - In order to import the trivial functions in a notebook — it works on a script as well — we have two things to do : ... To create a python file, you can use either the “new file” functionnality of your favorite IDE, or create it with the terminal typing the following command :
Find elsewhere
🌐
Python
docs.python.org › 3 › library › importlib.html
importlib — The implementation of import
One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Import Modules, Packages, Functions in Python | note.nkmk.me
May 12, 2023 - Check and add the module search path with sys.path in Python · As shown in the example of collections above, ModuleNotFoundError can also occur if you attempt to import a class instead of a module. In such cases, use from to import a function or a class.
🌐
W3Schools
w3schools.com › python › python_modules.asp
Python Modules
There is a built-in function to list all the function names (or variable names) in a module. The dir() function: List all the defined names belonging to the platform module: import platform x = dir(platform) print(x) Try it Yourself »
Top answer
1 of 9
275

Importing the module doesn't waste anything; the module is always fully imported (into the sys.modules mapping), so whether you use import sys or from sys import argv makes no odds.

The only difference between the two statements is what name is bound; import sys binds the name sys to the module (so sys -> sys.modules['sys']), while from sys import argv binds a different name, argv, pointing straight at the attribute contained inside of the module (so argv -> sys.modules['sys'].argv). The rest of the sys module is still there, whether you use anything else from the module or not.

There is also no performance difference between the two approaches. Yes, sys.argv has to look up two things; it has to look up sys in your global namespace (finds the module), then look up the attribute argv. And yes, by using from sys import argv you can skip the attribute lookup, since you already have a direct reference to the attribute. But the import statement still has to do that work, it looks up the same attribute when importing, and you'll only ever need to use argv once. If you had to use argv thousands of times in a loop, it could perhaps make a difference, but in this specific case it really does not.

Hence, the choice between one or the other should be based solely on coding style.

In a large module, I'd certainly use import sys; code documentation matters, and using sys.argv somewhere in a large module makes it much clearer what you are referring to than just argv ever would.

If the only place you use argv is in a '__main__' block to call a main() function, by all means use from sys import argv if you feel happier about that:

if __name__ == '__main__':
    from sys import argv
    main(argv)

I'd still use import sys there myself. All things being equal (and they are, exactly, in terms of performance and number of characters used to write it), that is just easier on the eye for me.

If you are importing something else altogether, then perhaps performance comes into play. But only if you use a specific name in a module many times over, in a critical loop for example. But then creating a local name (within a function) is going to be faster still:

 import somemodule

 def somefunction():
      localname = somemodule.somefunctionorother
      while test:
          # huge, critical loop
          foo = localname(bar)
2 of 9
62

There are two reasons in favor of using import module rather than from module import function.

First is the namespace. Importing a function into the global namespace risks name collisions.

Second isn't that relevant to standard modules, but significant for you own modules, especially during development. It's the option to reload() a module. Consider this:

from module import func
...
reload(module)
# func still points to the old code

On the other hand

import module
...
reload(module)
# module.func points to the new code

As for speed...

we are importing only the function that is needed rather than import the whole module (which contains more useless functions which python will waste time importing them)

Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. "Importing a function" is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.

🌐
Scaler
scaler.com › home › topics › __import__() in python
__import__() in Python - Scaler Topics
June 3, 2022 - The import in Python module helps in getting the code present in another module by either importing the function or code or file using the __import__() in Python method.
🌐
YouTube
youtube.com › automate with rakesh
How to import a specific function in Python | Python Tutorials for Beginners - YouTube
Learn how to import specific functions in python with this easy guide.The required module name must be used along with the import keyword. The import stateme...
Published: July 12, 2022
Views: 899
🌐
Programiz
programiz.com › python-programming › methods › built-in › __import__
Python __import__()
This function can be used to change the semantics of the import statement as the statement calls this function.
🌐
Python.org
discuss.python.org › python help
Importing a function - Python Help - Discussions on Python.org
July 22, 2024 - For some reason, I can’t import a function from another file. I’ve tried: # levels.py def level(): return 1 # main.py from levels import * print(level()) But it outputs an error: root@1de6ab8b7b69:~/my-project# …
🌐
Reddit
reddit.com › r/python › import inside functions
r/Python on Reddit: Import inside functions
January 5, 2019 -

Can anyone give me a good (technical) reason why I should not put import statements inside a function?

I use this a lot in python modules used by software that support python scripting. Especially in modules building custom menus to delay the imports just when the user run a function instead of importing tons of modules when the software launches. This helps me reduce the software startup time by a lot (in some case up to a minute if the software have a tons of custom tools and menus)

I always argue with colleagues about this and so far the only answer I get is “you are not suppose to do this”

Thanks

🌐
YouTube
youtube.com › case digital
How To Import Functions From Another Python File - YouTube
In this python tutorial, I show you how to import functions from another python file! I give you two different syntaxes you can use! Let's get coding!=======...
Published: January 22, 2024
Views: 611
🌐
Stack Abuse
stackabuse.com › bytes › how-to-import-all-functions-from-a-python-file
How to Import All Functions from a Python File
August 19, 2023 - This could be, for example, because there are simply too many functions to reasonably import one-by-one. In Python, you can access all functions of a module by importing the entire module. This is done using the import keyword followed by the module name. Once the module is imported, you can ...
🌐
YouTube
youtube.com › teachingcs
Python For Beginners - Importing Functions From Another File Explained - YouTube
00:00 - Start00:17 - Why you should modularise your code01:04 - How to make a new module02:17 - Importing functions from another file03:15 - Import just a sp...
Published: July 31, 2022
Views: 8K