Python3 doesn't see Django - python

When I attempt to verify that Python3 can see Django by entering python3 followed by import django into the terminal (so I can print Django's version number), I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'django'
Python can detect Django if I enter python (followed by import django), but not python3. How can I correct this so Python3 can also detect Django?
These are the steps I took to install Django on my local machine:
Upgraded pip:
sudo -H pip3 install --upgrade pip
Downloaded and installed latest version of Python from binary.
Created project directory:
mkdir django-start && cd $_
Installed virtualenv:
pip3 install virtualenv
Created an environment:
virtualenv env
Activated the environment:
source env/bin/activate
Installed Django:
pip install django

You virtualenv is built for Python 2 environment. Cmd python3 in virtualenv is still using the global Python which doesn't have django.
So if you want to use Python 3 in virtualenv you should add the -p or --python argument when initialize the env:
virtualenv --python $(which python3) env
Mind you need remove your old Python 2 env folder before you run this command.

Related

Python virtualenv installed package not found

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

Ensurepip module not existing, how to install manually?

In the interest of not getting an XY problem: the goal is to create a virtual environment on synology dsm, so no apt-get, where pip is installed manually.
I am trying to create a virtual environment, in above environment (synology dsm package python 3.8 with pip installed manually).
However this gives the following error:
$ python3 -m venv new_venv
Error: Command '['/volume1/docker/builder/new_venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
In the chain of finding that error I discovered that venv is working "just fine":
$ python3 -m venv --without-pip new_venv
Works as expected. Also pip itself works as expected. However I had to install pip manually. This also has as consequence that the synology dsm version of python does not have the module ensurepip..
# python3 -c "import ensurepip; print(ensurepip.__file__);"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'ensurepip'
This gives the problem: how does one manually install ensurepip, and/or make virtual env install pip without relying on ensurepip?
Install pip inside your venv virtual environment
Download the newest pip installation script and name the file get-pip.py:
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Create the virtual environment with Python 3 but without pip inside it (assuming you are in /volume1/docker/builder/):
$ python3 -m venv --without-pip /volume1/docker/builder/new_venv
Activate the virtual environment:
$ source /volume1/docker/builder/new_venv/bin/activate
Your prompt should now contain the virtual environment name in parens:
(new_venv) $
The script will install pip inside the activated venv virtual environment:
(new_venv) $ python get-pip.py
# or
(new_venv) $ python3 get-pip.py

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

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