İmport a python function inside a subfolder

In summary, to use a function from a file called my_functions.py in a file called test.py, you can use the importlib library and add the parent directory to sys.path. However, if you want to be able to run the script from anywhere, you will need to use the os.path library and add the parent directory to sys.path using the dirname and join functions.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
 
Technology news on Phys.org
  • #2
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
 
  • #5
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..?
 
  • #6
Arman777 said:
attempted relative import with no known parent package
You need to create an empty __init__.py file in Projects.
 
  • #7
Still gives an error..interesting
 
  • #8
Hmm, you may need __init__.py in Project 1 too
 
  • #9
Nope..it doest not work
 
  • #10
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
 
  • #11
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
 
  • #12
It does not work
 
  • #13
It does for me.

Screenshot at 2020-11-30 16-04-09.png


If you want it to work for you too then you need to be more specific.
 
  • #14
Its really interesting. It still says

ModuleNotFoundError: No module named 'my_functions'
 
  • #15
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).
 
  • #16
I am running 3.9. I am also using Windows. Maybe yeah...
 
  • #17
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 mfb, jim mcnamara, Wrichik Basu and 1 other person
  • #18
This time it works, thanks a lot.
 

Related to İmport a python function inside a subfolder

1. How do I import a python function from a subfolder?

To import a python function from a subfolder, you can use the dot notation. For example, if your function is located in a subfolder named "utils" within your main project folder, you can import it by using "from utils import function_name".

2. Can I import multiple functions from a subfolder at once?

Yes, you can import multiple functions from a subfolder by using the asterisk symbol (*). For example, "from utils import *". This will import all functions from the "utils" subfolder into your current file.

3. What if my subfolder has multiple levels?

If your subfolder has multiple levels, you can use the dot notation to specify the path to your function. For example, if your function is located in a subfolder named "utils" within a subfolder named "helper" within your main project folder, you can import it by using "from helper.utils import function_name".

4. How do I ensure that my subfolder is included in my python path?

You can ensure that your subfolder is included in your python path by adding an empty file named "__init__.py" to your subfolder. This will make it a python package and allows you to import functions from it.

5. Can I use relative paths to import my function from a subfolder?

Yes, you can use relative paths to import your function from a subfolder. For example, if your function is located in a subfolder named "utils" within your main project folder, you can use "from .utils import function_name" to import it using a relative path.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
8
Views
893
Replies
6
Views
722
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top