Import module from subfolder
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
Import module from subfolder in Python
The reason for this error is that types
is already a name used by Python for the standard library.
Rename your folder from types
to something else.
https://docs.python.org/3/library/types.html
In addition, you will probably have to change your imports to contain the full path, and execute it inside of the folder containing flowscanner
from flowscanner.types.Flow import Flow
Import a file from a subdirectory?
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.
ModuleNotFoundError when importing local module from sub folder in Python
You have to understand how Python finds the modules and packages when using import.
When you execute a python code, the folder in which this file is will be added to the PYTHONPATH which is the list of all the places where python will look for your packages and modules.
When you call caller1.py, it works because helper.py is in the same folder but it does not work for caller2.py because python does not find it in the same folder nor the other path in PYTHONPATH.
You have three options:
- call caller2.py from a script in the same folder as helper
- add the folder containing helper.py in the PYTHONPATH (not recommended)
- make your code into a package that you can
pip install -e
, this way python will be able to find your modules as they will be install in the site-packages of your python environment (most complex but cleanest solution)
Import a module from a subfolder and use files from that subfolder with the module
Something like:
import json
import os
configfile_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"config.json"
)
json.load(open(configfile_path, "r"))
Should do the trick
How to import a module from a subfolder in python3 (with empty __init__.py)
Let's suppose we have this folders/files architecture:
test
├── callfoo.py
└── folder
├── __init__.py
└── submodule.py
1 directory, 3 files
callfoo.py:
from folder.submodule import foo
def main():
foo()
if __name__ == '__main__':
main()
submodule.py:
def foo():
print('foo in submodule')
now place your self at the same level's folder of callfoo.py
and run:
$ python3 callfoo.py
Output:
> foo in submodule
How to Import module or a file from subfolder in Python?
After Doing some R&D, Have done it
Just need to create __init__.py
(a empty py file) and add it to all subdirectory
such as
/pyproject/
api/
__init__.py
gmail/
__init__.py
abc.py
facebook/
__init__.py
xyz.py
And add in main py file
from api.gmail.abc import *
or
import api.gmail.abc
Can't import subfolders from python module on GitHub
In the setup.py
you have to include the subfolder in the packages
as well. So, in setup.py
instead of:
packages=['package_folder']
You have to do:
packages=['package_folder', 'package_folder/subfolder1']
Related Topics
Understanding Python's Call-By-Object Style of Passing Function Arguments
Django: Deploying an Application on Heroku with SQLite3 as the Database
Flask Application Traceback Doesn't Show Up in Server Log
Connecting Slots and Signals in Pyqt4 in a Loop
How to Generate an HTML Directory List Using Python
Python Beautifulsoup Iframe Document HTML Extract
Django: How to Serve Media/Stylesheets and Link to Them Within Templates
Style Active Navigation Element with a Flask/Jinja2 MACro
Color Coding Cells in a Table Based on the Cell Value Using Jinja Templates
Pyinstaller Unable to Access Data Folder
Finding What Methods a Python Object Has
Explaining Python's '_Enter_' and '_Exit_'
Python Image Library Fails with Message "Decoder Jpeg Not Available" - Pil
How to Set the Current Working Directory
Why Is Numpy's Einsum Faster Than Numpy's Built in Functions
What Is the Cause of the Bad Request Error When Submitting Form in Flask Application