I have followed flask's tutorial to the point of installing the flaskr blog (here: http://flask.pocoo.org/docs/1.0/tutorial/install/). I'm new to installing python packages and I would appreciate some explanation of what's going on:
The tutorial says that installing the flaskr project has the benefit of being able to run it from anywhere.
However, I still can't run it outside the flask-tutorial directory (if I do flask run outside the flask-tutorial directory in the command line, I get flask.cli.NoAppException: Could not import "flaskr".)
Also, while I can import flaskr when working in the python interpreter, I also can't run it from there (even when I'm in the flask-tutorial directory):
import flaskr
app = flaskr.create_app()
app.run()
I get the following, and the python interpreter exits:
* Serving Flask app "flaskr" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
/Users/user/projects/flask-tutorial/venv/scripts/python.exe can't find '__main__' module in ''
--- UPDATE:
If I set $env:FLASK_ENV = "production", and then start the python interpreter, I can run flaskr successfully as described above.
However, still no luck running flaskr directly from the command line (with flask run) outside of the flask-tutorial directory.
You are unable to use it outside the project folder because you have installed it in editable mode (-e flag). Uninstall the package and install again without the -e switch, so that it will be installed globally on your system. After that, it should work.
Related
I have a discord bot written with python. But the catch is, it only works when deployed on heroku but doesnot run locally at all.
This is the folder structure
- feed
- __init__.py
- token.py
- main.py
-requirements.txt
When I run the command python3.9 feed/main.py, it gives the following error:
from feed import token
ModuleNotFoundError: No module named 'feed'
What is the issue here? Mind you that the code runs without any errors on heroku with the same command. I am on Ubuntu 21.04.
If I change feed to .feed, I get other errors regarding absolute imports.
Please don't redirect me to other answers, I have tried it all.
I don't know how Heroku works, but to get it running locally, try changing your import to:
import token
If you want to use feed as the parent directory, you can create a setup.py file and run pip install -e . in the folder. Then doing
from feed import token
should work as part of your development environment.
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…
I'm having trouble using requests module in my flask app. I have two files rest_server.py and independent.py at same directory level. The independent.py uses requests module and it executes correctly if I directly run it. But when I import independent.py in rest_server.py it shows following error
`
import independent
File "/home/satwik/Desktop/angelhack/independent.py", line 5, in <module>
import requests
ImportError: No module named requests`
I've tried pip install requests and it shows requirement already satisfied. Also I've tried to import requests in rest_server.py and found it to execute correctly too.
Here's my code
**independent.py **
`import json
import os
import sys
import requests
sys.path.append('/home/satwik/Desktop/angelhack/comprehensive_search')
** rest_server.py **
`#!flask/bin/python
import six
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask.ext.httpauth import HTTPBasicAuth
import independent
app = Flask(__name__, static_url_path="")`
How should I fix this?
Why you get the "no module named ..." error
Your two files have one big difference: rest_server.py includes a shebang line, while independent.py doesn't.
When you say you directly execute the file independent.py, you type python independent.py (I'm assuming here, because you didn't specify that). That means you are executing with the system python interpreter, which will look for modules installed at system level. Systemwide you have the requests module installed, via pip install requests, so python finds it, imports the thing and happily runs your script.
When you execute the file rest_server.py, instead, you can do so calling the script's name: ./rest_server.py (assuming correct permissions settings). In this case, the first line #!flask/bin/python (the so called shebang line) instructs to use a different python interpreter, the one contained in the flask folder, which I assume contains a virtual environment.
You get the no module named requests because that module is not installed inside the flask virtual environment.
How you can fix the error
To fix the problem, just install the requests module inside the virtual environment.
You first activate the virtual environment and then install the module you need:
$ source flask/bin/activate
$ pip install requests
Then you can try execute ./rest_server.py again and the requests module should be properly imported.
For more on the shebang line: https://en.wikipedia.org/wiki/Shebang_(Unix)
For more on virtual environments: https://pypi.python.org/pypi/virtualenv
Whenever you do pip install <package>, it installs the package to a certain location. Add that location to the list of PATHs mentioned in your Environment Variables, and your problem will be solved.
hi i had same problem but i solved it :
after you activate venv env by this command . venv/bin/activate
in this env u can type pip install requests
or
in ur project directory u can open pyvenv.cfg and turn
include-system-site-packages = false
to
include-system-site-packages = true
:)
I created a django project using cookiecutter as reccomended by Two scoops of Django 1.8. It's called icecreamratings_project
I use the git cmd prompt and use
'cd icecreamratings_project'.
When i want to use the built-in python interpreter by using
python manage.py shell it gives me the following error.
File "C:\Users\Armando\Desktop\icecreamratings_project\config\settings\common.py", line 13, in
import environ
ImportError: No module named 'environ'
I looked into the directory and the following code is there:
from __future__ import absolute_import, unicode_literals
from sys import path
import environ
ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path('twoscoops_project')
env = environ.Env()
No module named environ exists, but I'm assuming environ is in reference to the virtual environment. Im not familiar with the cookiecutter documentation or how it creates django templates, but i created a virtual environment named environ.
The message i got after that is that there is no Path in environ. Can someone help?
The environ module can be found in django-environ.
django-environ is a requirement of cookiecutter-django's requirements/base.txt.
base.txt is a requirement of cookiecutter-django's requirements/local.txt.
It seems you'll install environ and other needed modules by completing the following steps from cookiecutter-django's README.rst:
Getting up and running
The steps below will get you up and running with a local development
environment. We assume you have the following installed:
pip
virtualenv
PostgreSQL
First make sure to create and activate a virtualenv, then open a
terminal at the project root and install the requirements for local
development:
$ pip install -r requirements/local.txt
Source: https://github.com/pydanny/cookiecutter-django#getting-up-and-running
i'm trying to learn python pyramid for linux and following the pylonsproject documentation in doing so.I'm also new to linux.
I've installed everything correctly(I'm pretty sure), but when i invoke my helloworld.py, i got this following error
ImportError: No module named pyramid.config
the documentation says this should be the path to the file
$ /path/to/your/virtualenv/bin/python helloworld.py
but i'm confused as the python after the bin is a executable not a directory? my env is located in the Downloads folder
Thanks!
These symptoms are almost always a misuse of the virtualenv in one way or another.
1) Make sure you are running the virtualenv
$ env/bin/python helloworld.py
2) Make sure you installed pyramid into the virtualenv
$ env/bin/python
>>> import pyramid.config
# ImportError or not?