"Import flask from Flask" stops working after deactivating env - flask

already searched database within stockoverflow -- there are no answers to this question!
"Import flask from Flask" stops working after deactivating env
installed python3:
"python3 --version" -->returns Python 3.7.4
installed virtualenv
"pip3 install virtualenv"
"virtualenv env"
"virtualenv --version" --> returns 16.7.4
activated virtual environment
"source env/b/activate" --> creates (env) note at terminal prompt correctly
installed flask in virtual environment
" pip3 install flask"
In the python shell, import flask does not return an error.
python program is:
...
from flask import Flask
app = Flask(__ name __)
#app.route('/')
def hello_world():
- return 'Hello, World!'
...
I get an error!
7. the error is: "unable to import flask"
ANY IDEAS ARE APPRECIATED. THANK YOU
I am using the following to run program:
export FLASK_APP=flask_blog.py
pip show flask --> returns: flask not found
pip3 show flask --> returns: flask version 1.1.1
pip doesn't find flask but pip3 does. What does that mean?

virtualenv venv creates a virtual environment that uses Python 2. Since it isn't using Python 3, pip3 install flask uses the system pip3, which lives outside the virtual environment. This will install Flask outside of the virtual environment.
It's unclear how you're invoking Python. If you're typing python3, if you're getting the system python3, which will be able to import Flask.
Regardless, to correct this, remove venv, and build it again using
virtualenv --python=python3 venv
and then either activate the virtual environment, or use one of the wrappers that that virtual environment provides for you. e.g.,
venv/bin/pip install Flask
then
FLASK_APP=app.py venv/bin/flask run

Related

how do solve import error in django project

ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable i have activated the virtualenv on many but yet I can't run the server
When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things.
Just reinstall Django in the virtual environment:
pip install Django
and then just run the command for testing:
python manage.py runsever

Django 2.1.1 Import Errors

i use vscode on windows10. I just created a Django project+app like this:
In the vscode powershell terminal:
python -m venv venv #createing a virtual environment called venv
.\scripts\activate #activate the virtual environment
pip install django==2.1.1 #install the newest version of Django
python -m pip install --upgrade pip #upgrading pip
pip install requests #install requests for api-requests
django-admin startproject api_order . #creates a django project called api_order
python manage.py migrate #create database
python manage.py runserver #starting the server
because of the server running in my powershell terminal i take a second powershell terminal and continue with activating the venv and creating a app.
.\scripts\activate
python manage.py startapp api_order_app #creating a app called api_order_app
When i now open the models.py python is reporting an problem "E0401:Unable to import 'django.db'" Same for all other imports in every other script.
What did go wrong?
I did this twice always the same problem.
This is how it looks like in the IDE
<img src="https://i.stack.imgur.com/JTmvb.jpg">
Thanks in Advance for helping me out.
Ben
I figured it out... I wasnt on the right virtualenvironment. But Django was only installed at the venv.
In Visual Studio Code you can choose the environment u want to use in the bottom left corner.
Now everything works just fine.

Flask module cannot be found in pyvenv although it is installed

I used the following commands to create the virtual environment and run my code:
mkdir ~/.virtualenvs
pyvenv ~/.virtualenvs/bitirme
source ~/.virtualenvs/bitirme/bin/activate
pip3 install flask
pip3 install httpie
python3 server.py
Although flask is installed correctly I still get the error that flask module cannot be found. What am I doing wrong here?
Try to do this in the normal way (i guess, that you on *NIX):
Install "virtualenv" module via pip
Go to the directory, where you wanna create virtual environment
Type in terminal "virtualenv %name% %python_path%"
Now to activate, do "./%name%/bin/activate"
Install "pip install flask httpie"
Run "python server.py"
NOTE:
If you created virtual environment with your "python 3"
installation, you should not add "3" to python, or pip

Running django in virtualenv - ImportError: No module named django.core.management?

I have installed Django after activating my virtualenv but still I am getting following error
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
If you already activated your virtualenv (source /path/bin/activate) then check if you have installed Django.
pip install django
With next command you can see if Django was installed.
pip freeze | grep django
Another thing that you can try is to remove first line (#!/usr/bin/env python) in the manage.py file.
You should check if django is installed
Activate your environment, then run the following command to see which version is installed :
python -c "import django; print(django.get_version())"
I am using virtual environment so I added this line in manage.py:
sys.path.append('./myvenv/lib/python3.5/site-packages')
in which myvenv is the name of my virtual environment and version of my installed Python is 3.5.
This solved my issued.
I found that I had Python 3.4 and 2.7 installed concurrently, and the pip install django==1.7 command automagically decided the Python 3.4 /dist-packages was where it should live. I CD'd over to the Python 2.7 directory and re-piped it... and all is well.
sudo pip install django --upgrade
worked for me, i am not having virutal environment by the way.
I had the same problem when I was running Django from inside a virtual environment and then using another terminal window ran the command
python manage.py shell without first switching to the venv.
The problem was resolved after I switched back.
I found that I could import the django module from the python interpreter, but django-admin.py could not import it when run from the command line.
I confirmed that I was using the python interpreter in my virtual environment.
I was using a 64-bit version of python. Uninstalling, and installing the 32-bit version, then re-creating my venv solved this for me.
If you're using virtualenv, you can add it to your path using sys.path.append('./myvenv/lib/python3.5/site-packages').
Try closing and opening the terminal again. That worked for me too.

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following error:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105