Flask module cannot be found in pyvenv although it is installed - flask

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

Related

Django Error on django-admin startproject

I try this command:
$ virtualenv .venv
$ sourse .venv/bin/activate
(.venv) $ pip3 install django
The last command installed the django3 on the whole system while the virtual machine was active and should only be installed on the virtual machine. why??
I tried django-admin stratproject mysite, but I received this error:
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
so, I try sudo apt install python-django-common. Then again try django-admin stratproject mysite and resived error: Cannot find installed version of python-django or python3-django.
how I can solve this problem??
Did you add a Python path. If you have, you can enter the Python terminal when you write Python. You can see the installed libraries by typing pip freeze.
The Django is not added in your python path , if you still want to run your code go to python directory and then run the django-admin command.
Go to the folder where your python is installed and open terminal there
after that run your command
django-admin startproject test
Try using pipinstall django instead of pip3 install django in virtualenv.
By running virtualenv .venv you have created local python environment with current default system version of python (i.e. on older distributions this might be python 2.7)
While activating virtual environment, it replaces some ENV variables to allow python to address this local environment, but not system one.
pip is one of them. pip inside virtualenv points to virtualenv's pip. Virtualenv might not redefine pip3 command - this is only needed system-wide, cause you may have many different versions of python in the system at the same time. And as far as virtualenv has only one version of python / packages inside - where is no need for it to redefine pip3.
Running django-admin startproject test inside virtualenv uses virtualenv's python packages, and if there is none, it may actually use system-wide packages, but it depends on the options virtualenv was created with (use system pacakges), PATH variables etc.
Also, try not to install or rely on system-wide packages - use virtuelnv.
Using virtualenv is good, and there is a lot of helpers to ease - pipenv, virtualenwrapper etc
Just run this in the command line.
apt-get install python3-django
Hope this will solve your problem.

"Import flask from Flask" stops working after deactivating env

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

how to install pip remotely using ssh

Hi I hope someone can point me in the right direction.
I am trying to upload a django project which I have developed locally on my machine and now moved the project files to a server and am trying to install django on the server.
I have Python 2.7.5 installed and accessed the server remotely using ssh (putty) I can confirm Python is installed by running the command python --version
I don't have pip installed as when i run the command pip --version
I get following notification
-bash: pip: command not found
I am new to django and python so not sure what I should do to install both django and pip.
p.s In my requirements file and when working locally I have pip and django installed correctly and all working.
Ok, lets say you are already on your remote server. First thing to do is to install pip for your version of python. You can do this via:
sudo apt-get install python-pip
From now you have pip installed. Next thing to do is to install django globally in your system:
pip install django==1.11
Please note that django 1.11 is the last version that supports
python2
Next thing to do is to create django app:
django-admin startproject test_project
And the last thing is to install virtualenv
To install libraries for each of your django projects and keep them
separate
pip install virtualenv
Also note
If you have requirements.txt file with all libs, you can do something like this on your remote server:
pip install -r requirements.txt
That will automatically install all libraries at once
First you should understand which OS you're running:
uname -a
and:
lsb_release -a
When you find the OS version, you can easily follow this guide:
https://packaging.python.org/guides/installing-using-linux-tools/#installing-pip-setuptools-wheel-with-linux-package-managers

Can't find any modules in Django project

I've been following a Django tutorial and initially created virtualenv and a requirements file in it. Project was halfway and in working state. Today I activated virtualenv successfully and tried python manage.py runserver to get error
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
I realized it's not finding Django and just to be sure checked my requirements.txt file (which confirmed right Django version). Tried to reinstall it from the file using pip3 install -r requirements.txt (tried using just pip too) to get another error --
ModuleNotFoundError: No module named 'pip'
It seems the environment is unable to find any of the modules. I tried to reinstall pip also just to be sure, but then it couldn't find apt-get command.
OS: Mac OSX El Capitan 10.11.6
Python: 3.6.2
Django: 1.10.3 (in requirements file)
Try running python -m ensurepip (or python3 -m ensurepip) to see if pip is already installed:
In most cases, end users of Python shouldn’t need to invoke this module directly (as pip should be bootstrapped by default), but it may be needed if installing pip was skipped when installing Python (or when creating a virtual environment) or after explicitly uninstalling pip.

Django not installed in venv?

Would anyone know possible reasons why Django is being installed in the global site package and not my venv's site package folder?
Here's my set up and what I did, this is a bit detailed since I'm new to Python/Django and not sure which information is important:
Python 3.3 is installed in c:\python33
I have virtualenv, pip, easy_install installed in C:\Python33\Scripts.
My venv is c:\users\username\projects\projB
This venv was created using pyvenv, not virtualenv.
I activated the venv.
I changed directory to C:\Python33\Scripts to run "pip install django".
Django was created inside C:\Python33\Lib\site-packages and not inside C:\users\username\projects\projB\Lib\site-packages.
Do I need to install pip inside my venv and use that to install Django?
You can specify in your virutalenv wich python version you whant:
$ virtualenv -p <PATH TO PYTHON VERSION> my_virtualenv
Then:
$ source my_virtualenv/bin/activate
$ pip install Django==1.5.2
This will install the good version of django in your virtualenv according to your python version.
Thanks to virtualanv, you will be able to save/freeze and install your environement on another machine:
$ pip freeze > requirement.txt
$ pip install -r requirement.txt
You will see in the requirement.txt file the django dependency.
Pip should be installed when you create the virtual environment. Don't change directory into C:\Python33\Scripts before running pip. It looks like that means you use the base install's pip instead of your virtual environment's pip.
You should be able to run pip from any other directory. However I'm not familiar with python on Windows, so I'm not certain that pip is added to the path when you activate the environment. If that doesn't work, you'll have to change directory into the bin directory of your virtual environment, then run pip.
What happened to me was that I was trying to install django from outside the environment directory/folder.
So make sure you are inside the environment directory and then use pip install django