Python virtualenv installed package not found - python

I cannot import djangoin my fresh virtualenv installation (python 3.7.9).
So far :
$ virtualenv env
$ source env\bin\activate
$ (env) pip install django
$ (env) pip freeze
asgiref==3.3.1 Django==3.1.7 pytz==2021.1 sqlparse==0.4.1
All good so far.
Except:
$ (env) python
>>> import django
ModuleNotFoundError: No module named 'django'
I've tried :
where django-admin
my_website/env/bin/django-admin
So clealy if my command line can recognise it but not python, it has to do with PYTHONPATH.. I'm just not sure how to proceed now, it gets very confusing from here.
Note : I've also aliased my python version to 3.7.9 in bash.
$ (env) python --version Python 3.7.9

when activating the virtual environment, use following commands to install Django
# install django
python -m pip install django
# get pip freeze
python -m pip freeze

Related

How to make sure pip installs packages to python 3?

I am seeing some weird stuff on my system:
$ pip install python-binance-api
...Library/Python/2.7/lib/python/site-packages (from python-binance-api) (3.13.2)
...
python versions:
$ python --version
Python 2.7.14
10:05 PM ~/kittycapital add_more_curr
$ python3 --version
Python 3.6.4
My pip is still installing to python 2.7 when I want it to install on python3. How do I get it to do this?
If I want the commands pip and python to point to python 3.7, what can I do?
Solution 1
python3 -m pip install xxx
# for example: python3.7 -m pip install requests
Solution 2
virtualenv --python=python3.7 venv
source venv/bin/activate
pip install requests
Solution 3
pipenv --python 3.7 # python3 -m pip install pipenv --user
pipenv shell
pipenv install requests

virtualenv with new Python and old set of packages

I have my environment setup on CentOS 6, comes with default Python 2.6.6, and the Django version in production is 1.4.1
Recently, started working on porting the app to latest Long Term Support Django 1.8. Locally I have refactored the app to make it ready. Issue arose when pushed the changes to production, setup a vituralenv with Django 1.8, on basic test importing django gave exception. The underlying cause is straightforward i.e. Django 1.8 at minimum sports Python 2.7
The question is I need to setup a virtualenv with Python 2.7.* and Django 1.8, on the same host where Python 2.6.6 is in use as a production environment Python interpreter, such that the new venv (with Python 2.7.* and Django 1.8) contains all the currently installed Python packages (I have lot lot of them).
The best possible way I have found:
Install python 2.7.*.
Use this newly installed python for virtualenv.
Copy the site-packages from python 2.6.6
New Python 2.7.8 - Create a new dir for fresh Python installtion
$ cd /var/www/myproject
$ mkdir new_py
$ cd /var/www/ or cd ..
Donwload, untar, and install Python 2.7.8
$ wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
$ tar -zxvf Python-2.7.8.tar.gz
$ cd Python-2.7.8
$ ./configure --prefix=/var/www/myproject/new_py/Python-2.7.8/
$ make && make install
Creating new venv in project direcroty i.e. myproject (replace it with your own project name) with newly installed Python 2.7.8 - specified with -p flag
$ virtualenv -p /var/www/myproject/new_py/Python-2.7.8/bin/python2.7 venv_djangoupgrade
New python executable in venv_py2.7.8/bin/python2.7
Also creating executable in venv_py2.7.8/bin/python
Installing setuptools, pip, wheel...done.
Copy the installed Python packages - (Or, the technical way is to get the list of installed packages $ pip freeze > requirements.txt , and once the venv is activated execute $ pip install -r requirements.txt it'll install the list of packages)
$ cp -R /usr/lib/python2.6/site-packages/* var/www/myproject/venv_djangoupgrade/lib/python2.7/site-packages/
For debian i.e. Ubuntu
$ cp -R /usr/lib/python2.6/dist-packages/* /var/www/myproject/venv_py2.7_django1.8/lib/python2.7/site-packages/
Activate the venv
$ source djangoupgrade_venv/bin/activate
(djangoupgrade_venv) $
In case you just didn't copy the site-packages but the pip freeze list, execute the following pip command while the venv is active
(djangoupgrade_venv) $ pip install -r requirements.txt
and finally install/upgrade Django
(djangoupgrade_venv) $ pip install -U django==1.8
Execute the above commands as root or use sudo

Why is python saying I have "no module named venv"?

I installed virtual env with sudo pip install virtualenv but when I run python -m venv flask I'm still getting this: /usr/bin/python: No module named venv
Versions, if that's relevant:
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
Python 2.7.9
what am I missing here?
Since you are on Python 2, you need to execute using the virtualenv module that you installed.
First step, as you originally tried to do, but this time you specify the "virtualenv" module and the name of the virtualenv. In this case flask:
python -m virtualenv flask
Then you activate your virtualenv like this:
source flask/bin/activate
Then install flask with pip inside the virtualenv
pip install flask
If you want to deactivate your virtualenv, simply type:
deactivate
If running on Python 3, the venv command is built-in and you can simply do:
python3 -m venv flask
Note, depending on how your Python 3 is installed, your python execution command might differ. You could be running it as python3, python3.5, python3.6.
venv is a module introduced in python3
venv is New in version 3.3.
The venv is ony available in python 3 version. If you are using python 2 then try to use virtualenv instead of venv.
1. Install virtualenv,
python -m pip install virtualenv
2. Create a virtual environment named venv using virtualenv,
Python 2
python -m virtualenv venv
Python3
python -m venv venv
3. Activate virtual environment,
.\venv\Scripts\activate.bat
4. Install flask package,
pip install flask
If are you using "windows".
Try it in "cmd"
navigate in cmd to the folder you want to install venv and do:
python3 -m venv project_env
You can change the name of the project to.
I changed python -> python3:
python3 -m venv flask
For python3 users, just be sure that you installed pip and venv packages:
sudo apt install python3-pip
sudo apt install python3-venv
Then you can use it like this:
python3 -m venv ~/sample
. ~/sample/bin/activate
pip install flask
Do the following for this issue.
pip install venv
(If this has got some issue that means python version recognized by your machine don't have updated version) so use below command:
pip install the virtualenv
python -m venv <>
if this has got the same issue so use below one.
python -m virtualenv <>
sudo apt-get install python3-pip
python3 -m pip install virtualenv
python3 -m virtualenv venv
source venv/bin/activate

Can't Install Django

I can't install Django, I read that pip is included in python 3.4, but when I write following command:
pip install django
this error appears:
SyntaxError: invalid syntax.
What I should do? Am I missing something? I'm using Python 3.4.2
From the SyntaxError: invalid syntax it looks like you're typing it in at the python interpreter (the >>>). pip install django needs to be run from a terminal.
pip is installed when you install python but it is a program in its own right not a python package/module. Docs are here
Since Python 3.4, pip installer is included by default with the Python binary installers.
~$ python -m pip install django
Read the docs:
pip included with Python
Installing Python Modules
or (on Debian/Ubuntu)
~$ apt-get install python3-pip
I recommend using virtualenv for you django project, which includes pip. You can install virtualenv by:
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ [sudo] python setup.py install
Then cd to a folder you want to create a django project:
$ virtualenv ENV
then simply run
$ source bin/activate
You should be able to pip install django from there

ImportError: No module named virtualenv

I am using Django 1.3.7 and python 2.7.6 on windows7
I got an error when I executing my manage.py in this line of code
import shutil, sys, virtualenv, subprocess
amd running it, I got this error
C:\Django-Proj\>python manage.py update_ve
Traceback (most recent call last):
File "manage.py", line 4, in <module>
import shutil, sys, virtualenv, subprocess
ImportError: No module named virtualenv
Does anyone have an Idea about my case?
Install virtualenv using pip install virtualenv.
If you have it already installed, try reinstalling it by removing it with pip uninstall virtualenv and then reinstalling it.
Good Luck.
I had to install virtualenv with the -H flag to set HOME variable to target user's home dir.
sudo -H pip install virtualenv
I think the problem is you need sudo to globally install virtualenv.
> pip install virtualenv
Could not find an activated virtualenv (required).
> sudo pip install virtualenv
Downloading/unpacking virtualenv
...
But this creates files readable only by root (depending on the umask).
In this case, uninstalling/reinstalling may not always help.
You can check with ls -la /usr/local/lib/python2.7/dist-packages/virtualenv.py (replacing 2.7 with whatever version you have or are targeting).
My solution was simply:
sudo chmod -R o+rX /usr/local/lib/python2.7
Use pip3 instead of pip. I had the same issue and pip3 worked for me.
$ pip3 install virtualenv
$ virtualenv venv --python=python3
Try
python3 -m pip uninstall virtualenv
python3 -m pip install virtualenv
I just ran into this same problem. I had to pip uninstall virtualenv as a user with admin rights, then pip install virtualenv as a normal user. I think it's some kind of permissions issue if you installed virtualenv under admin rights.
>virtualenv
ImportError: No module named 'virtualenv'
>pip uninstall virtualenv
PermissionError: [Errno 13] Permission denied:
>sudo pip uninstall virtualenv
Successfully uninstalled virtualenv-15.1.0
>pip install virtualenv
Collecting virtualenv
>virtualenv
Options:
Bingo!
I had the same problem when I created my virtualenv via pycharm and installed requirements with pycharm.
After trail and error , I found that installed requirements are not taken into account by the virtualenv.
The solution is to reinstall all requirements once you have activated your virtualenv:
venv\scripts\activate
python -m pip install -r YourRequirements.txt
Next time I'd better create my virtualenv directly with command line
Got this error when using the ansible pip module automating some pip installs on my localhost.
fatal: [localhost]: FAILED! => {"changed": false, "cmd": ["/opt/bin/virtualenv", "--system-site-packages", "-p/usr/bin/python3", "/opt/venv/myenv"], "msg": "\n:stderr: /usr/bin/python3: No module named virtualenv\n"}
Uninstalling virtualenv python3 -m pip uninstall virtualenv did show virtualenv was installed here /home/ubuntu/.local/bin/virtualenv.
In the ansible task specify the virtualenv_command:
- name: install requirements file
pip:
virtualenv_command: "/home/{{whoami.stdout}}/.local/bin/virtualenv"
virtualenv: "/home/{{whoami.stdout}}/.venv/{{item.env.virtualenv}}"
requirements: "/home/{{whoami.stdout}}/git/{{item.env.requirements_txt}}"
virtualenv_site_packages: yes
when: req_stat.stat.exists
For mac os the issue was with virtualenv. This is because the folder virtualenv did not exist.
This worked well
python3 -m venv env

Categories