Take a look at the Packages documentation (Section 6.4).

In short, you need to put a blank file named

__init__.py

in the lib directory.

🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Importing python code from a sub-folder - Raspberry Pi Forums
import ../../write_to_db.write_sensor as write def DoRead(): n = someFunction() write.DoWrite(n) I don't think you can do that, but you can pass a function pointer down from the top level, something like ... ./main.py ... import read_sensors.read_sensors as read import write_to_db.write_sensor as write read.DoRead( write.DoWrite ) ./read_sensors/read_sensors.py ... The 'neateast' way is probably using python module structure but it's additional complexity if you've not worked with modules before.
Discussions

Is there a cleaner way of importing modules from a different subfolder within a repository?
That is quite a pickle you have created there. Let's look at this as a user problem for a minute, the user in this case being the "less technical users" that need access to those scripts. How does that work for them right now? Do they use the console to call those scripts? They at the very least need to call the code somehow that you printed above, how does that happen? I'm asking, since you don't want to introduce a breaking change for them, so understanding the setup from their perspective is important. More on reddit.com
🌐 r/learnpython
8
6
January 14, 2023
python - Import module from subfolder - Stack Overflow
0 Importing another module from another subdirectory of the current directory's parent directory (python) 57 Relative importing modules from parent folder subfolder More on stackoverflow.com
🌐 stackoverflow.com
Importing function from separate file in subfolder
I am trying to format a project of mine and ran into a TON of issues trying to format the folders. If someone could help me out that would be… More on reddit.com
🌐 r/learnpython
3
1
December 10, 2022
import - Importing from subfolder in Python - Stack Overflow
This is the situation. I'm using Python 3.6 I currently have the next folder organization: /MainProject __init__.py /Folder1 pyscript1.py pyscript2.py __init__.py /Fol... More on stackoverflow.com
🌐 stackoverflow.com
June 8, 2020
People also ask

How do I import a module from a subdirectory in Python?
Make the directory a package when appropriate, use its package-qualified import path, and run the application from the project root with python -m.
🌐
pythonpool.com
pythonpool.com › home › tutorials › import python code from a subdirectory: packages, paths, and -m
Import Python Code from a Subdirectory: Packages, Paths, and -m
Should I modify sys.path to import a subdirectory?
It can be useful for controlled tooling, but application code is usually more maintainable with a package, installed project, or explicit environment configuration.
🌐
pythonpool.com
pythonpool.com › home › tutorials › import python code from a subdirectory: packages, paths, and -m
Import Python Code from a Subdirectory: Packages, Paths, and -m
What does __init__.py do?
It can mark and configure a regular package and expose selected names, although modern namespace packages can also work without it under specific layout rules.
🌐
pythonpool.com
pythonpool.com › home › tutorials › import python code from a subdirectory: packages, paths, and -m
Import Python Code from a Subdirectory: Packages, Paths, and -m
🌐
Sentry
sentry.io › sentry answers › python › import files from a different folder in python
Python Import From Parent Directory and Subfolders | Sentry
Import modules from parent, child, and sibling directories using package relative imports with __init__.py or by adding paths to sys.path at runtime
🌐
CodersLegacy
coderslegacy.com › home › how to import python files from subdirectories
How to Import Python Files from Subdirectories - CodersLegacy
July 23, 2023 - The most straightforward way to import Python files from subdirectories is by using absolute imports.
🌐
Reddit
reddit.com › r/learnpython › is there a cleaner way of importing modules from a different subfolder within a repository?
r/learnpython on Reddit: Is there a cleaner way of importing modules from a different subfolder within a repository?
January 14, 2023 -

I currently have a project which is just a repository of various scripts for various purposes, all organized by folder structure. Unfortunately, with some of the custom modules I have written, Ive run into the problem of importing them properly, since they are nested within a different folder higher up the folder tree.

My current solution is hideous, which adds these lines at the top of every script:

import os, sys
print(os.path.dirname(sys.argv[0]))
root_folder = str(os.path.dirname(sys.argv[0])).split("REPONAME")[0] + "REPONAME"
sys.path.insert(0, root_folder)

It crawls up the file tree to find the 'root folder' of my repository, and adds it to my path before my other imports so that it can be seen by the script when it runs.

A few suggestions Ive seen as I was searching for solutions was to set them up in the site-packages folder or to use pipenv install -e . inside of the folder of modules.

My concern with those solutions is the 'portability' of it, as this repository is being deployed across a department of less technical users who are trained on simply running the scripts. I need something simple that we can manage as the ones who create the script without adding an extra step of technical setup for people on the other end.

Top answer
1 of 6
200

There's no need to mess with your PYTHONPATH or sys.path here.

To properly use absolute imports in a package you should include the "root" packagename as well, e.g.:

from dirFoo.dirFoo1.foo1 import Foo1
from dirFoo.dirFoo2.foo2 import Foo2

Or you can use relative imports:

from .dirfoo1.foo1 import Foo1
from .dirfoo2.foo2 import Foo2
2 of 6
28

Just to notify here. (from a newbee, keviv22)

Never and ever for the sake of your own good, name the folders or files with symbols like "-" or "_". If you did so, you may face few issues. like mine, say, though your command for importing is correct, you wont be able to successfully import the desired files which are available inside such named folders.

Invalid Folder namings as follows:

  • Generic-Classes-Folder
  • Generic_Classes_Folder

valid Folder namings for above:

  • GenericClassesFolder or Genericclassesfolder or genericClassesFolder (or like this without any spaces or special symbols among the words)

What mistake I did:

consider the file structure.

Parent
   . __init__.py
   . Setup
     .. __init__.py
     .. Generic-Class-Folder
        ... __init__.py
        ... targetClass.py
   . Check
     .. __init__.py
     .. testFile.py

What I wanted to do?

  • from testFile.py, I wanted to import the 'targetClass.py' file inside the Generic-Class-Folder file to use the function named "functionExecute" in 'targetClass.py' file

What command I did?

  • from 'testFile.py', wrote command, from Core.Generic-Class-Folder.targetClass import functionExecute
  • Got errors like SyntaxError: invalid syntax

Tried many searches and viewed many stackoverflow questions and unable to decide what went wrong. I cross checked my files multiple times, i used __init__.py file, inserted environment path and hugely worried what went wrong......

And after a long long long time, i figured this out while talking with a friend of mine. I am little stupid to use such naming conventions. I should never use space or special symbols to define a name for any folder or file. So, this is what I wanted to convey. Have a good day!

(sorry for the huge post over this... just letting my frustrations go.... :) Thanks!)

Find elsewhere
🌐
Python Pool
pythonpool.com › home › tutorials › import python code from a subdirectory: packages, paths, and -m
Import Python Code from a Subdirectory: Packages, Paths, and -m
July 13, 2026 - Import Python modules from a subdirectory using packages, __init__.py, absolute and relative imports, PYTHONPATH, and python -m without fragile path hacks.
🌐
Delft Stack
delftstack.com › home › howto › python › python import from subdirectory
How to Import Module From Subdirectory in Python | Delft Stack
March 11, 2025 - By organizing your code into subdirectories, you can create a more modular structure, but you’ll need to ensure that Python can find these modules. One of the most common ways to import a module from a subdirectory is by using an __init__.py file. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-import-module-from-different-directory
Python – Import module from different directory - GeeksforGeeks
August 3, 2026 - We can manually add the folder path to Python’s search path using the sys module. ... # main.py import sys sys.path.insert(0, "../Folder_2") # add Folder_2 path to search list import module1 print(module1.add(5, 3)) # Output: 8 module1.odd_even(10) # Output: Even
🌐
GitHub
gist.github.com › CMCDragonkai › fe342b958e013078d72500d286973075
Importing Python Packages as Subdirectories #python · GitHub
Just replace RepositoryName with the name of the repository of the package. Now you can use from package import module within your own modules.
🌐
Reddit
reddit.com › r/learnpython › importing function from separate file in subfolder
r/learnpython on Reddit: Importing function from separate file in subfolder
December 10, 2022 - Is there a cleaner way of importing modules from a different subfolder within a repository? r/learnpython • · upvotes · · comments · I’ve been working on a python project for 2 years. I still have to look up the documentation for most functions. Is this normal?
🌐
Reddit
reddit.com › r/learnpython › how to import from a sub folder?
r/learnpython on Reddit: How to import from a sub folder?
January 22, 2021 -

I'm familiar with creating files and importing them, but I've gotten to a point where I want to keep my code in different folders in order to avoid clutter.

I have, however, been incapable of doing so.

The main folder is called chapters. It has a sub folder named code, and inside it is a file named info.py

I've tried using "from code import info" and similar phrases, to no avail. I've also tried importing sys and appending to path, which a google search said was the way to go. No success there, either. Finally, I tried another suggested solution, which was to create an empty filed named __init__.py , which didn't work either.

Any help would be greatly appreciated :D

🌐
Physics Forums
physicsforums.com › more sciences and computing › programming and computer science
Importing multiple classes from GRTensors subpackage with __init__.py • Physics Forums
July 12, 2021 - In any case I don't need any PYTHONPATH stuff so there's no problem on my side. ... It seems that I don't As long as you don't have any modules at all in the Objects folder itself (i.e., all that is there is subfolders like GRTensors), so you never have to import anything from Objects, yes, this will work, since Python has supported implicit namespace packages since version 3.3: https://www.python.org/dev/peps/pep-0420/ So what Python is doing is interpreting "Objects" as a namespace package, because that folder doesn't have an __init__.py but it has subfolders that do.
🌐
YouTube
youtube.com › watch
PYTHON : Import module from subfolder - YouTube
PYTHON : Import module from subfolder [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Import module from subfolder Not...
Published: December 6, 2021
🌐
MLJAR
mljar.com › answers › import-files-from-different-folder-python
Import files from different folder in Python
To import files from parent or sibling directories, you can use package relative imports. First, you have to creaty an empty file named __init__.py in each directory to make them recognized as packages by Python:
🌐
Better Stack
betterstack.com › community › questions › importing-files-from-different-folders-python
Importing files from different folder in Python? | Better Stack Community
Alternatively, you can use the PYTHONPATH environment variable to add the path to the folder containing the file you want to import. You can also use the importlib.import_module() function to import modules from different folders by providing the full path of the module.
🌐
YouTube
youtube.com › watch
Python - Importing your modules (Part 2: Import from a different folder ~7 mins! no ads) - YouTube
Hey guys, In this episode I’m going to demonstrate how to import your own python packages within various folders relative to your main python file. Feel free...
Published: July 20, 2020
🌐
McNeel Forum
discourse.mcneel.com › rhino developer
Modules from specified sub-folders not recognized/loaded - Rhino Developer - McNeel Forum
July 14, 2021 - Hi there, this might be a bit embarrassing, but I am having trouble with importing custom modules. The search paths are specified in the options of the PythonEditor of course. C:/folder1 C:/folder1/subfolder The supe…