Flask server does not recognize my own imported module (ModuleNotFoundError) - python

In my main Python file, I import another script of mine called helper_1.py (from the subfolder my_helpers) like this:
from my_helpers.helper_1 as h1
However, when I now try to start my server (or deploy it to Heroku), the server will crash with the error notice:
ModuleNotFoundError: No module named 'my_helpers'
I do have a Procfile, requirements.txt, runtime.txt, and wsgi.py.
The content of my wsgi.py is:
from app.main import app
if __name__ == "__main__":
app.run()
MY QUESTION:
Where and how do I have to declare my custom modules (own scripts) so they are properly detected when starting the Flask server?
Everything works fine if I leave out the external reference to my custom module.

The folder my_helpers needs to be a package. To do this put an __init__.py file inside the my_helpers folder. This maybe fix your problem.

Related

IIS can not access newly added files in a django project

In a django project that is served by IIS (windows), i added a local file, test.py. The project ran perfectly before and still runs perfect on localhost, however IIS appears to not recognize the new test.py file. It appears IIS access to this file fails, even if the users IUSR and IIS_USRS have full access (same as for all other files in the folder).
I get below error message, and somethimes also the same but "No module named 'app.test'. Removing the "import app.test as test" in views.py solves the issue.
Suprisingly the "import app.oldFile as oldFile" works without issue.
In my views.py, i import the python scripts like this
import app.oldFile as oldFile
import app.test as test
My django project has the structure:
djangoRest
-app
--__init__.py
--views.py
--oldFile.py
--test.py
-djangoRest
--__init__.py
--settings.py
--urls.py
--wsgi.py
I found the reason, and solution.
In the earlier import file oldFile.py i use the line
import os
os.chdir(r\\some\\other\\directory\\)
This obviously changes the current working directory and as a result the other file test.py can't be found, and cannot be imported.
This mistake does not matter when testing django on the localhost, but only manifests in an issue when served by IIS.

im trying to host my flask api on heroku, but when passing the path to my app, i get an import error

the error im getting: ImportError: attempted relative import with no known parent package
My folder structure:
-Backend
__init__.py
run.py
procfile
__init.py:
has the create app method
run.py:
from . import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
procfile:
web: gunicorn run:app
EDIT:
I rearranged the app structure by which:
Backend
init.py
procifle
extensions.py
src
init.py
run.py
init.py:
empty
src/init.py:
from ..extensions import db,migrate
has the create app method
src/run.py:
from src import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
so now the new error is:
from ..extensions import db
ImportError: attempted relative import beyond top-level package
Edit #2:
another thing to point out is, that suppose in the run.py i do the following:
from Backend import create_app()
I get the following error: no module named "Backend" why could that be?
Has anyone faced a similar issue and what can I do to solve it?
Update based on new information I think we can get you home.
1.) Move run.py next to the procfile (same dir). That "run file" should be in that top level dit at /app/run.py in Heroku.
A good basic pattern for file organization is to "module" everything (a dir with a __init__.py file). Then all imports are from the top level run.py [where the process is running from] walking subfolders. As long as you traverse down into imports things stay pretty stable. If you are trying to navigate up dirs it becomes problematic.
Resolve any imports, it can be tricky at first but soon becomes a way of thinking.
2.) Move extensions.py into src (for now) and import from src import foo
The following is the pattern in all my active Django and similar apps running in Heroku. Same layout I have been using for many years. In the following screenshot manage.py is the Django equivalent of your run.py. I am not using the docker image on this app, so that is not in the equation, just code and dependencies.
Your problems are totally understandable, it's easy to take for granted all the times other Python devs do this.

How to import app file into test file in different directory Python

apologies if I'm going about this wrong, but I am quite new to python and can't quite figure out what the problem is! I have a simple Flask app that I'm trying to write tests using pytest for. The file structure is like this:
│── app.py
|── tests
│ ├── tests_app.py
I originally had app.py and tests_app.py in the same level and it all worked fine with tests passing, but now I have put tests in their own folder I can no longer import app without error.
I have tried the following in tests_app.py:
from app import app #this is what worked fine when app and tests were in the same folder
from ..app import app
from .. import app
and the linting error is 'Attempted relative import beyond top-level package' and when I run pytest it says 'ImportError: attempted relative import with no known parent package'.
Many thanks in advance!
p.s. I am using Python 3.8.5
You need to have app on the module search path. A quick fix would be to add the path to app.py to the PYTHONPATH environment variable:
export PYTHONPATH=/path/to/dir # where app.py is
If app.py is on the python path, then import app will work (barring any other import problems such as circular imports).
An effective and reproducible way to do this is to install your application. You can do this in develop mode, which means the code runs from its current directory. This is an understated, important part of developing a Python application, but to do it you will need to write code to package your application.

importing from another folder in virtualenv

I'm following the Flask Mega Tutorial, and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask.
My current folder structure is as follows
/FlaskTest
/app
/static, templates etc
/flask
/virtualenv folders etc
/tmp
run.py
as far as I can tell, the folder structures are identical other than naming of the top level directory.
in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial,
from flask import Flask
app = Flask(__name__)
from app import views
I'm getting an Import Error that "cannot import name 'Flask'". I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages.
My question: How can I reference the sub folder of flask/site-packages?
I've read through the python import system documentation and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect.
UPDATE: So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. I ran pip install flask in the app directory. Now my site runs, but I'm not sure if this is the best practice of doing things with Python. Please provide some clarity as what the best practice is for installing packages and where the packages reside.
UPDATE 2: After creating a directory called standalone. In this folder, I created a virtual environment called standalone-test. Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. I was able to run the run.py script by using python run.py, but I can't run python -m app like you had said without running into an error. The error is as follows if it helps.
"No module name app.main; 'app' is a package and cannot be directly executed.
I am able to run python run.py as I mentioned, but I'm not able to run the python -m app command as you had mentioned
I think something went wrong in your execution environment. Here are some explanations.
The virtualenv
See the documentation of virtualenv
If you have followed the tutorial:
The flask directory is your virtualenv,
On posix system, you have a flask/bin subdirectory, or
On Windows system, you have a flask\Scripts subdirectory.
I make the assumption that you are on posix system.
To activate your virtualenv, run:
source flask/bin/activate
Your prompt should change to something like: (flask)$.
To list the installed libraries use pip:
pip list
Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something…
If Flask is missing, install it:
pip install Flask
Run your app
Your application is in the app directory, it has an __init__.py file (it's a Python package).
In this file, you have:
from flask import Flask
app = Flask(__name__)
from app import views
From your FlaskTest/ directory, try to run this script like this:
cd FlaskTest/ # if not in this directory
python -m app
This should import Flask, instanciate your app (but don't run it), import the views module.
If app/views.py exist you should have no error.
=> at this point, we have simulated what run.py imports…
Now write run.py in your FlaskTest/ directory:
#!flask/bin/python
from app import app
app.run(debug=True)
Run it like this:
python run.py
Note that the shebang #!flask/bin/python is unusual, but should work in the context of the tutorial.
This should start your http server…

Import a module from the root

In this structure:
app
-companies
--models.py
--__init__.py
-manage.py
-__init__.py
models.py
class Company():
pass
manage.py
from flask import Flask
app = Flask(__name__)
from app.companies.models import Company
if __name__ == '__main__':
manager.run()
ImportError: No module named app.companies.models
What is the reason? In the views.py, inside the companies folder, the import works without any problem.
The problem is not with your code, but with how you're running it:
I am running python manage.py
For this to work, your working directory must be app. The current working directory is automatically added to your sys.path. There are two possibilities here, both of them bad:
The more likely is that the directory containing app isn't on sys.path at all, so as far as Python is concerned, there is no top-level package named app.
The less likely is that the directory containing app is on sys.path, as well as app itself. In that case, you will confuse the hell out of the importer, because the same file or directory can end up imported as two or more separate objects with different names in sys.modules. It's hard to predict exactly what problems that can cause, but it will cause problems.
Either way, the solution is to not run it that way. Instead, run it from one directory up, the directory that app is in. Which means you have to run it one of the following ways:
python -m app.manage
python app/manage.py

Categories