flask cli can't locate the script manage.py - python

my flask app is a package named app located at /Users/gexinjie/Codes/MyProject/xinnjie_blog
the file tree is like this
xinnjie_blog
├── app
| ├── __init__.py
│   ├── config.py
│   ├── exceptions.py
│   ├── model.py
│   ├── model_sqlalchemy.py
│   ├── static
│   ├── templates
│   ├── util.py
│   └── views
├── manage.py
I export it as PATHONPATH, so manage.py can import app
echo $PATHONPATH
/Users/gexinjie/Codes/MyProject/xinnjie_blog
and export FLASK_APP
echo $FLASK_APP
manage.py
current dir is /Users/gexinjie/Codes/MyProject/xinnjie_blog
pwd
/Users/gexinjie/Codes/MyProject/xinnjie_blog
here is the manage.py
import click
from app import create_app
app = create_app('development')
#app.cli.command()
def initdb():
click.echo('Init the db...')
here is app.__init__.py
from flask import Flask
from .model_sqlalchemy import db
def create_app(config_name='default'):
app = Flask(__name__)
... # init
return app
but then if I execute flask initdb, I get this error:
Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb".
and if I execute flask run, I get
Usage: flask run [OPTIONS]
Error: The file/path provided (manage) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
why manage.py is not found? And how can I fix it.
(actually it worked well when manage.py have flask app in itself )
# manage.py
# this work well
app = Flask(__name__) # not app = create_app('development')
Thank you

Thank to #Adam, This problem was solved after I uninstall Anaconda.
Because all the time I tested manage.py on Pycharm command tool, and that flask was installed by Anaconda(python version 3.6), it may lack some extensions(usually I use python3.5 on terminal).So I think the problem occur during importing.
The flask command tool complain about 'can't locate the app', but the real problem is import error.So that is so confusing.
"universal" solution:
So when if you come to this kind of problem like I do, I suggest you first check the location of your app(try both relative path and absolute path), relative path may cause the locate problem when you are not at the right working directory.So absolute path is recommend.
If every thing about path went well, then make sure that all the packages required by your app is installed and can be imported. if you are using some type of virtual environment or something like that (In my case, I use the other version of python lacking some flask extensions), it may be the import error makes flask complain.
Hope this can help you.

Related

uwisg: ModuleNotFoundError: No module named 'flask'

I am trying to make a flask application, and I am following the well known tutorials out there. I am using docker containers, so I don't need virtual environments, as the containers are already isolated environments.
that's my project folder for the flask app:
flask
├── Dockerfile
├── app
│   ├── __init__.py
│   ├── routes.py
│   ├── static
│   └── templates
│   ├── index.html
│   └── test.html
├── requirements.txt
├── run.py
└── uwsgi.ini
When starting up the container with docker-compose, I get the following error.
Traceback (most recent call last):
File "run.py", line 1, in <module>
from app import app #as application
File "./app/__init__.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
unable to load app 0 (mountpoint='') (callable not found or import error)
I have read many other questions with such an error, but none of those solutions helped or had to do with venv (which I am not using). So I hope someone can pinpoint, where the problem might be.
I have installed flask in Dockerfile with
RUN python3 -m pip install --disable-pip-version-check --no-cache-dir -r /tmp/requirements.txt
(I also just used pip3 install... but it does not make a difference, flask is installed either way). from the build log:
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 flask-2.0.2 itsdangerous-2.0.1
## run.py
from app import app
This is the import that seems to fail:
## __init__.py
from flask import Flask
app = Flask(__name__)
from app import routes
extract from uwisg.ini:
[uwsgi]
plugin = python3
wsgi-file = run.py
callable = app
After starting the container with docker-compose, I exec into the container, start a Python3 interactive shell and have no problems at all doing from flask import Flask.
I have tried running the container as root but that did not change anything.
Any ideas? thanks!
Ok, after more rinse and repeat trying, I figured it out.
In my Dockerfile I also set the PYTHONPATH to my workspace:
ENV PYTHONPATH "${PYTHONPATH}:/workspace/flask"
ENV PYTHONPATH "${PYTHONPATH}:/workspace/flask/app"
and site-packages apperently could not be accessed by Python anymore.
I also had to add this:
ENV PYTHONPATH "${PYTHONPATH}:/usr/local/lib/python3.11/site-packages"
I have no idea, why this path was not accessible for Python? Did I overwrite sys.path when using ENV PYTHONPATH? Should this not be additive to sys.path?

how to run a script using pyproject.toml settings and poetry?

I am using poetry to create .whl files.
I have an ftp sever runing on a remote host.
I wrote a python script (log_revision.py) which save in a database the git commit, few more parameters and in the end send the the .whl(that poetry created) to the remote server ( each .whl in a different path in the server, the path is save in the db) .
At the moment I run the script manually after each time I run the poetry build commend.
I know the pyproject.toml has the [tool.poetry.scripts] but i dont get how can i use it to run a python script.
I tried
[tool.poetry.scripts]
my-script = "my_package_name:log_revision.py
and then poetry run my-script but I allways get an error
AttributeError: module 'my_package_namen' has no attribute 'log_revision'
1. can some one please help me understand how to run to wish commend?
as a short term option(with out git and params) i tried to use the poetry publish -r http://192.168.1.xxx/home/whl -u hello -p world but i get the following error
[RuntimeError]
Repository http://192.168.1.xxx/home/whl is not defined
2. what am i doing wring and how can i fix it?
would appricate any help, thx!
At the moment the [tool.poetry.scripts] sections is equivalent to setuptools console_scripts.
So the argument must be a valid module and method name. Let's imagine within your package my_package, you have log_revision.py, which has a method start(). Then you have to write:
[tool.poetry.scripts]
my-script = "my_package.log_revision:start"
Here's a complete example:
You should have this folder structure:
my_package
├── my_package
│   ├── __init__.py
│   └── log_revision.py
└── pyproject.toml
The content of pyproject.toml is:
[tool.poetry]
name = "my_package"
version = "0.1.0"
description = ""
authors = ["Your Name <you#example.com>"]
[tool.poetry.dependencies]
python = "^3.8"
[tool.poetry.scripts]
my-script = "my_package.log_revision:start"
[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
and of log_revision.py:
def start():
print("Hello")
After you have run poetry install once you should be able to do this:
$ poetry run my-script
Hello
You cannot pass something to the start() method directly. Instead you can use command line arguments and parse them, e.g. with pythons argparse.
Although the previous answers are correct, they are a bit complicated. The simplest way to run a python script with poetry is as follows:
poetry run python myscript.py
If you are using a dev framework like streamlit you can use
poetry run streamlit run myapp.py
Basically anything you put after poetry run will execute from the poetry virtual environment.
For future visitors, I think what OP is asking for (a post build hook?) isn't directly supported. But you might find satisfaction from using a tool I wrote called poethepoet which integrates with poetry to run arbitrary tasks defined in the pyproject.toml in terms of shell commands or by referencing python functions.
For example you could define something like the following in your pyproject.toml
[tool.poe.tasks.log_revision]
script = "my_package.log_revision:main" # where main is the name of the python function in the log_revision module
help = "Register this revision in the catalog db"
[tool.poe.tasks.build]
cmd = "poetry build"
help = "Build the project"
[tool.poe.tasks.shipit]
sequence = ["build", "log_revision"]
help = "Build and deploy"
And then execute and of the tasks with the poe CLI like the following which will run the other two tasks in sequence, thus building your project and running the deployment script in one go!
poe shipit
By default tasks are executed inside the poetry managed virtualenv (like using poetry run) so the python script can use dev dependencies.
If you need to define CLI arguments or load values into a task from a dotenv file (such as credentials) then this is also supported.
Update: poetry plugin support
poethepoet can now support post build hooks when used as a poetry plugin. For example when using poetry >=1.2.0b1 you can configure the following to run your log_revision task automatically after poetry build is run:
[tool.poe.poetry_hooks]
post_build = "log-revision"
[tool.poe.tasks.log-revision]
script = "scripts:log_revision"
Tinkering with such a problem for a couple of hours and found a solution
I had a task to start the django server via poetry script.
Here are the directories. manage.py is in test folder:
├── pyproject.toml
├── README.rst
├── runserver.py
├── test
│   ├── db.sqlite3
│   ├── manage.py
│   └── test
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── tests
│   ├── __init__.py
│   └── test_tmp.py
└── tmp
└── __init__.py
I had to create a file runserver.py:
import subprocess
def djtest():
cmd =['python', 'test/manage.py', 'runserver']
subprocess.run(cmd)
then write the script itself pyproject.toml:
[tool.poetry.scripts]
dj = "runserver:djtest"
and still make changes to pyproject.toml:
[tool.poetry.scripts]
dj = "runserver:djtest"
only then command poetry run dj started working

AWS Chalice using custom config path or custom app path?

I am trying to use Chalice to fit into an pre-existing build folder structure, the python source file (app.py) is one level deeper than the vanilla Chalice project
├── .chalice
│   └── config.json
└── src
├── app.py
├── requirements.txt
├── requirements_test.txt
When I run chalice local in src folder it says it cannot find the config file:
Unable to load the project config file. Are you sure this is a chalice project?
When I run chalice local in the project folder, it says it cannot find the source file:
No module named 'app'
I had a look at the config file doesn't seem to have an option to specify where the source file is.
Since there were no responses on Stackoverflow, so I went to the project and opened an issue ticket for it, from the horses mouth, this is not possible at this stage.

Flask can't find the config module in parent directory

I'm basing myself on the structure of a web app I found up on github, here.
My project's structure looks like this:
~/Learning/flask-celery $ tree
.
├── config
│   ├── __init__.py
│   └── settings.py
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── web
├── app.py
├── __init__.py
├── static
└── templates
└── index.html
I want my Flask app in web/app.py to load the settings in the config module, as I saw in the githug project linked above.
Here's how I'm instantiating the Flask app in web/app.py:
from flask import Flask, request, render_template, session, flash, redirect, url_for, jsonify
[...]
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.settings')
app.config.from_pyfile('settings.py')
[...]
The issue I'm getting is:
root#0e221733b3d1:/usr/src/app# python3 web/app.py
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/werkzeug/utils.py", line 427, in import_string
module = __import__(module_name, None, None, [obj_name])
ImportError: No module named 'config'
[...]
Obviously, Flask can't find the config module in the parent directory, which makes sens to me, but I don't understand how the linked project I'm basing myself on is successfully loading the module with the same tree structure and Flask config code.
In these circumstances, how can I get Flask to load the config module?
Without adding the config directory to your path your Python package will not be able to see it.
Your code can only access what is in web by the looks of it.
You can add the config directory to the package like so:
import os
import sys
import inspect
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)
Then you should be able to import config.
In case anyone finds themselves looking at this question from years ago, it is possible to load the config file in the project structure above like this:
app.config.from_pyfile('../config/settings.py')
This will work when starting the app using flask run

Why does django not see my tests?

I've created test.py module, filled with
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from forum.models import *
class SimpleTest(TestCase):
def setUp(self):
u = User.objects.create_user("ak", "ak#abc.org", "pwd")
Forum.objects.create(title="forum")
Site.objects.create(domain="test.org", name="test.org")
def content_test(self, url, values):
"""Get content of url and test that each of items in `values` list is present."""
r = self.c.get(url)
self.assertEquals(r.status_code, 200)
for v in values:
self.assertTrue(v in r.content)
def test(self):
self.c = Client()
self.c.login(username="ak", password="pwd")
self.content_test("/forum/", ['forum'])
....
and placed it in folder with my application.
When i run tests by
python manage.py test forum
after creating the test database i get an answer "Ran 0 tests in 0.000s"
What am i doing wrong ?
P.S.
Here is my project hierarchy:
MyProj:
forum (it's my app):
manage.py
models.py
views.py
tests.py
...
I renamed test.py to tests.py. Eclipse got that this module is with tests, but answer is still "Ran 0 tests in 0.000s"
You´ll need to use the prefix test_ for each test method.
Summary
Try running for your app only:
python manage.py test YOUR_APP
Check in your settings.py file if YOUR_APP is in INSTALLED_APPS config.
Test method should start with test_, e.g.:
def test_something(self):
self.assertEquals(1, 2)
If you are using a directory called tests instead of the tests.py file, check if it has a __init__.py file inside it.
If you are using a tests directory, remove tests.pyc and tests.pyo files. (__pycache__ dir for Python3)
Try renaming your method test to something like test_content.
I believe the test runner will run all methods named test_* (see the python docs for organising test code. Django's TestCase is a subclass of unittest.TestCase, so the same rules should apply.
You have to name it tests.py .
After some time spent in searching I did not find anyone suggesting this, so I will share it as a late answer.
In my case I have my manage.py in the root directory eg
.
...
├── dir
│   ├── project_name
│   ├── manage.py
│   ├── media
│   ├── app1
│   ├── static
│   ├── staticfiles
│   ├── templates
│   └── app2
...
so I found that the test command has the option to provide project which tests' to run. In my case I had to do this
python project_name/manage.py test ./project_name/
This successfully ran my tests.
There is something not quite right if you are getting the same result after renaming the file to tests.py. How are you running the tests? Are you doing so from the command line or have you set up a custom run target using Eclipse? Please try it from the command line if you haven't already.
Also fire up Django shell (python manage.py shell) and import your tests module.
from MyProj.forum.tests import SimpleTest
Does the import work correctly?
I had tried all these things but I neglected to add the __init__.py file in the tests directory that I created to hold all my tests and Django couldn't find it.

Categories