I'm building a Flask app with Python 3.5 following a tutorial, based on different import rules. By looking for similar questions, I managed to solve an ImportError based on importing from a nested folder by adding the folder to the path, but I keep failing at importing a function from a script in the same folder (already in the path). The folder structure is this:
DoubleDibz
├── app
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ └── helloworld.py
│ ├── app.py
│ ├── common
│ │ ├── __init__.py
│ │ └── constants.py
│ ├── config.py
│ ├── extensions.py
│ ├── static
│ └── templates
└── run.py
In app.py I import a function from config.py by using this code:
import config as Config
but I get this error:
ImportError: No module named 'config'
I don't understand what's the problem, being the two files in the same folder.
Have you tried
import app.config as Config
It did the trick for me.
To import from the same folder you can do:
from .config import function_or_class_in_config_file
or to import the full config with the alias as you asked:
from ..app import config as Config
# imports all functions
import config
# you invoke it this way
config.my_function()
or
# import specific function
from config import my_function
# you invoke it this way
my_function()
If the app.py is invoked not from the same folder you can do this:
# csfp - current_script_folder_path
csfp = os.path.abspath(os.path.dirname(__file__))
if csfp not in sys.path:
sys.path.insert(0, csfp)
# import it and invoke it by one of the ways described above
Another, shorter way would be:
import .config as Config
Related
I have the following structure:
│
├── pckg1/
│ └── utils/
│ ├── module11.py
│ └── module12.py
│
├── pckg2/
│ └── utils/
│ ├── module21.py
│ └── module22.py
│
└── main_pckg/
├── utils/
└── main.py
When I run main.py, I import required methods from both module11.py and module21.py under utils directories under from pckg1 and pckg2.
In side those modules there are some local imports. For Example (pckg1/utils/module11.py):
from utils.module21 import X,Y,Z
when I run main_pckg/main.py, the following error naturally raises:
No module named `utils/module21` under the current working dir.
Since there is no module21
A naive solution is to modify those imports to relative import:
from ..utils.module21 import X,Y,Z
But I have many modules and subprojects with are tracked and shall not be modified.
Any workarounds to solve it?
Python is trying to reach those files from main.py.
You can add those folders to path from main file.
import sys
sys.path.append('../pckg1/utils')
sys.path.append('../pckg2/utils')
After that you won't need to specify the folder name
from module21 import x, y, z
should work
In test_app, I have tried appending '../../src/app.py' to sys.path, from ...src.app import function or from app import function but I keep getting ModuleNotFoundError: No module named 'app'. Not sure how to overcome this seemingly simple issue.
├── src
│ └── app.py
├── tests
│ └── unit_tests
│ ├── __init__.py
│ ├── conftest.py
│ └── test_app.py
You need to add the full path to the parent folder, then import app:
import sys
sys.path.append("../../src")
# We can now directly access 'app' as we have a pointer to its parent directory
from app import function
# Use 'function' here
CONTEXT. Python 3.9.1, Flask 2.0.1 and Windows 10
WHAT I WANT TO DO. Call a module located inside a custom package.
TROUBLE. No matter what I do, the console insists on telling me: ModuleNotFoundError: No module named 'my_package'
WHAT AM I DOING.
I am following the official Flask tutorial https://flask.palletsprojects.com/en/2.0.x/tutorial/index.html. Therefore, the structure of the project is basically this (with slight changes from the tutorial):
/flask-tutorial
├── flaskr/
│ ├── __init__.py
│ ├── db.py
│ ├── schema.sql
│ ├── templates/
│ ├── static/
│ └── my_package/
│ ├── __init__.py (this file is empty)
│ ├── auth.py
│ ├── backend.py
│ ├── blog.py
│ └── user.py
├── venv/
├── .env
├── .flaskenv
├── setup.py
└── MANIFEST.in
Inside the file /flask-tutorial/flaskr/init.py I try to call the modules that contain my blueprints:
from my_package import backend
from my_package import auth
from my_package import user
from my_package import blog
backend.bp.register_blueprint(auth.bp)
backend.bp.register_blueprint(user.bp)
backend.bp.register_blueprint(blog.bp)
app.register_blueprint(backend.bp)
When I running the application (with the flask run command), the console tells me ModuleNotFoundError: No module named 'my_package', indicating that the problem is in the line where it is from my_package import backend.
HOW I TRIED TO SOLVE THE PROBLEM (unsuccessfully).
First. I have created an empty file called init.py inside 'my_package'
Second. Inside the file /flask-tutorial/flaskr/init.py, before the problematic FROM, I have put:
import os.path
import sys
parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent)
On the internet there are several posts with this same problem. Many of them mention that the solution is to put a init.py file inside the package (I already did this and it does not work for me). Also, unfortunately the structure of the projects in question is different from mine.
Any ideas how to fix this?
Change your code to
from .my_package import backend
from .my_package import auth
from .my_package import user
from .my_package import blog
backend.bp.register_blueprint(auth.bp)
backend.bp.register_blueprint(user.bp)
backend.bp.register_blueprint(blog.bp)
app.register_blueprint(backend.bp)
Just add a single dot . before the package name to import the package in the same parent package. In your case, __init__.py and my_package have a common flaskr as their parent package.
I have a directory like
.
└── my_project
├── API
│ ├── __init__.py
│ └── admin.py
└── my_project
├── __init__.py
└── module1_file.py
Inside API/admin.py, I want to import func1 from my_project/module1_file.py. I do like this:
import sys
sys.path.insert(0, './myproject/myproject/')
from module1_file import func1
It throws me this error
from module1_file import func1
ModuleNotFoundError: No module named 'module1_file'
Can anyone please show me how to solve this? Thanks.
I'm building a website with the bottle framework. I am using nosetests for my unit testing. However there is a problem I can't solve on my own.
I can't seem to find a way that allows me to run the tests without breaking my server. When I run my tests, I have to do relative imports for them to work, but they don't work when I start the server.
This is my folder structure:
├── service
│ ├── __init__.py
│ ├── application.py
│ ├── main.py
│ ├── response_header.py
│ ├── global_data_loader.py
│ ├── renderer
│ │ ├── __init__.py
│ │ ├── header.py
│ │ ├── ....
│ ├── tests
│ │ ├── test_application.py
│ │ ├── ....
Here is how I import so my server works:
application.py --
from response_header import ResponseHeader
from global_data_loader import GlobalDataLoader
from renderer import Application
However, when I run nosetests, I get this message:
ModuleNotFoundError: No module named 'response_header'
So when I want nosetests to work, I have to change the imports to look like this:
application.py --
from .response_header import ResponseHeader
from .global_data_loader import GlobalDataLoader
from .renderer import Application
Then my nosetests work, but when I want to start my server I get this message:
from .response_header import ResponseHeader
ImportError: attempted relative import with no known parent package
I have tried using sys.path.append in my tests, and it works, but I need a solution that doesn't involve PYTHONPATH or using os/sys
edit: i fixed it by moving my main.py I use to start the server to the root folder, above my service and tests module. now it works fine