İmport a python function inside a subfolder

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Function Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
17 replies · 7K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have a project folder called Projects and inside that folder I have a file called my_functions.py. Now I have a folder named Project 1
and inside there is another python file called test.pySo it is something like

-Projects-
my_functions.py
-Project 1-
test.py

Now I have some functions in the my_functions.py file such as f1, f2 etc.

Now, I need to take a function from my_functions.py and use it inside the test.py

I have used things like

from my_functions import f1
from .my_functions import f1
from Projects.my_functions import f1

For the first one I get an error called

ModuleNotFoundError: No module named 'my_functions'

I have searched online and I find that I need to do something about _init_.py somewhere (?) The problem is, I am new in codding so I could not find where to put that module, in what way etc. So if someone can tell me what to do step by step it would be really helpful.
Thanks Thanks
 
on Phys.org
Based on this example on the importlib documentation:

Python:
import importlib.util
import sys

module_name = 'my_functions'
file_path = '/path/to/Projects/my_functions.py'

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

# This should now work:
my_functions.f1

# This may or may not work
from my_functions import f1
 
pasmith said:
Based on this example on the importlib documentation:

Python:
import importlib.util
import sys

module_name = 'my_functions'
file_path = '/path/to/Projects/my_functions.py'

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

# This should now work:
my_functions.f1

# This may or may not work
from my_functions import f1
Is there a simpler way..?
 
1606746852083.png


This is how it looks like. I need to write something that takes a function from my_functions and works in the test.py
 
Oh, right. You shouldn't need the __init__.py, you just need to add the parent directory to sys.path, either from the command line or in code:
Python:
# myProjects/test.py
import sys
sys.path.insert(0, '..')
from my_functions import f1
 
Its really interesting. It still says

ModuleNotFoundError: No module named 'my_functions'
 
Could possibly be a Windows issue - I don't have access to a Windows machine with Python on until later today. Or possibly a Python version issue: relative imports did change in about v3.3 I think (as you can see that example is running in 3.8.5).
 
Just tested it in Windows, it works for me there too - then I realized why. I am running test.py from the myProjects folder using python test.py. I think you are running it from the parent folder using python myProjects/test.py.

Paths in python scripts are relative to the path python is run from, not the path of the current file. You could either run it from the project folder or change the path command to sys.path.insert(0, '.'), but the best way to fix it so you can run it from anywhere is
Python:
# myProjects/test.py

from os.path import dirname, join
import sys

sys.path.insert(0, join(dirname(__file__), '..'))

from my_functions import f1

print(f1(1))
 
  • Like
  • Informative
Likes   Reactions: mfb, jim mcnamara, Wrichik Basu and 1 other person