Installing Django on Ubuntu 16.04, python3 - python

I am unable to install django on python3 in ubuntu 16.04.
Here is what I have tried:
1. pip3 install django
2. pip3 install --trusted-host pypi.python.org django
3. pip3 install --index-url=http://pypi.python.org/simple --trusted-host pypi.python.org django
I keep getting the same error:
Could not fetch URL https://pypi.python.org/simple/django/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
Could not find a version that satisfies the requirement django (from versions: )
No matching distribution found for django
I have Django installed on python 2.7, but I need it on python 3.6.
When I run
sudo apt-get install python3-django
it says
python3-django is already the newest version (1.8.7-1ubuntu5.5).
I believe this is because I have python3.5 installed with Django on python3.5, but I need it on 3.6. Python3 refers python3.6. My pip3 is up to date.
Any help is appreciated.

1st: You didn't explicitly say so, but I am assuming you are using linux based on your mention of apt-get
The simplest way to get django on your python 3.6 is to fix pip. This is a pretty good overview of how to get the libraries that pip needs based on error messages such as yours. You just need to apt install a few packages.
Once you have all the dependencies installed for pip3 to run, try installing django again.
Note: since you have python3 already, I'd make sure you are calling the right pip3, you can do this by calling:
pip3 -V
If the 'pip3' command is calling the pip3 in 3.5, then use the full path of the pip3 in python 3.6 instead.
Alternatively
You can try copying django from your lib/site-packages folder from your python3.5 installation to your python3.6 installation. The big gotcha is that you need to make sure to copy all the dependencies for django as well. You can ether look them up in the django config or you can try and use it and copy them over one at a time based on the error messages.
Unsolicited Advice:
I would strongly suggest using virtualenvs to make this process much easier. I use pyenv and pyenv-virtualenv here, and have really loved them.

16.04 is a LTS release. As such it is locked to python 3.5. You may have noticed that there were no packages in the Xenial repositories and had to install python 3.6 from alternate sources. Just be careful with that since things can break at the system level. In the end I went with building python from source and generating a django venv using.
python3.6 -m venv mydjangoproject
See this post for more details.
See this gist for a working Ubuntu16.04 Python 3.6.3 example

#allow to add latest python version-
sudo add-apt-repository ppa:deadsnakes/ppa
#update-
sudo apt update
#installing python3.6 and pip package manager-
sudo apt install python3.6 python3-pip
#update for alternative version of python-
sudo update-alternatives --install /usr/bin/python3 python3/usr/bin/python3.6 1
#configuring python 3.6-
sudo update-alternatives --config python3
#install django -
sudo apt install python3-django
#adding project name-
django-admin startproject projectname
#cd to project dir-
cd projectname
#add your ip add using vim or any editor in settings.py inside []-
nano ~/projectname/projectname/settings.py
#Run the server-
python3 manage.py runserver 0.0.0.0:8000
#run at browser using 127.0.0.1:8000

Related

Change python version in google colab which is connected with google cloud

I am using python 3.7 in google colab, but for some reason when I am connected with a linux server in google cloud the python becomes 2.7. How can I change it? Note that python 3.7 is already installled in the server.
I tried these things but weren't helpful.
apt update
sudo apt install python3-pip
alias pip='pip3'
I checked also these sites https://cloud.google.com/python/docs/setup#linux_2, How do I install Python 3.7 in google cloud shell but didn't solve my problem.
Edit
By doing Runtime --> Change runtime I can see only this, which the options are 'non', 'gpu', 'tpu'.
You can check your Python version at the command line by running python --version. In Colab, we can enforce the Python version by clicking Runtime -> Change Runtime Type and selecting python3.
Python 2.7 is expected to be removed in Debian "testing", the basis of gLinux. This is expected to happen shortly after the next major version, Debian Bullseye, is released.
You need to migrate Python 2.7 code to Python 3 and remove python-is-python2 packages you might have installed.
When Python 2.7 is removed from gLinux, python-is-python2 will be uninstalled.
If you have software that requires /usr/bin/python to work, but can be used with Python 3, you should install python-is-python3.
You can change Python version by running the following commands in terminal:
pip install virtualenv
virtualenv venv --python=python3
This only works if you have Python2.7 installed at the system level (e.g. /usr/bin/python2.7).
You can find the path to your Python installation with
which python3
virtualenv venv --python=/usr/local/bin/python3
And check the version using python3 –version.
So actually the answer of my question comes from this post How to completely uninstall python 2.7.13 on Ubuntu 16.04.
Everyone who have a similar problem the first thing that he/she should do is to unistall python 2.7 by using either
sudo apt install --reinstall python python-apt python2.7-minimal
or
sudo apt purge python2.x-minimal
and then install python 3.7, by using
sudo ln -s /usr/bin/python3 /usr/bin/python
sudo apt install -y python3-pip
sudo ln -s /usr/bin/pip3 /usr/bin/pip
# Confirm the new version of Python: 3
python --version

Installed Virtualenv and activating virtualenv doesn't work

I cloned my Django Project from Github Account and activated the virtualenv using famous command source nameofenv/bin/activate
And when I run python manage.py runserver
It gives me an error saying:
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 was thinking that every and each dependency I need, might be present inside virtualenv.
Well, no. By default, a newly created virtualenv comes empty, that is, with no third-party library. (Optionaly, you may allow a virtualenv to access libraries installed system-wide, but that's another story.)
Once the virtualenv is created, you need to install the dependencies you need.
(How could virtualenv know what dependencies you need?)
The procedure is to install the virtualenv, activate it, and then install the libraries needed for the project (in you case Django and perhaps others).
If you project has a requirements.txt, you may install every required dependency with the command:
pip install -r requirements.txt
If your project has a setup.py, you may also execute
pip install -e path/to/your/project/clone/.
to install the project in the virtualenv. This should install the dependencies.
Of course, if the only dependency is Django, you can just type
pip install django
on ubuntu version
#install python pip
sudo apt-get install python-pip
#install python virtualenv
sudo apt-get install python-virtualenv
# create virtual env
virtualenv myenv
#activate the virtualenv
. myenv/bin/activate
#install django inside virtualenv
pip install django
#create a new django project
django-admin.py startproject mysite
#enter to the folder of the new django project
cd mysite
#run the django project
python manage.py runserver
If you have several python on your machine, for example,python2.7, python3.4, python3.6, it is import to figure out which version the python really reference to, and more over, which version does pip reference to.
The same problem got in my way after I installed the let's encrypt when I run the following command.
(python3 manage.py runserver 0:8000 &)
I inspected the python version and found that python3, python3.4, python3.6, python3.4m were all available.
I just change python3 to python3.6 and solved the problem.
(python3.6 manage.py runserver 0:8000 &)
So, this is probably a version mismatching problem if it is OK for a long time and crashes down suddenly.
I'm guessing you also upload the virtual environment from your other pc. And you hope that only activating that will work, bzz.
It's not recommended to upload the virtualenv files to your git repository, as #Alain says it's a good practice to have a requirements.txt file containing the project dependencies. You can use pip freeze > requirements.txt (when the environment is activated) to generate the project requirements file.
By doing so, when you clone the repository from another computer, you need to create a new virtualenv by issuing the command:
virtualenv nameofenv
then activate it:
source nameofenv/bin/activate
and finally, use the requirements file to install the requirements for your project using:
pip install -r requirements.txt
I had installed Django 2 via pip3 install Django, but I was running python manage.py runserver instead of python3 manage.py runserver. Django 2 only works with python 3+.

Install python 2.6 in a sandbox/virt-env

I have python 2.7 running on ubuntu 14.04. and, I need to set up py2.6 in a sandbox environment. I tried using the command, virtualenv as
virtualenv /path/to/sandbox --no-site-packages
But, it copies /usr/bin/python2.7 binary file into the sandbox's bin folder.
Using pythonbrew also didn't work, as it throws compilation errors almost always.
How to create a sandbox environment and install python2.6 binary in it?
Virtualenv won't really install a new python version from scratch, but rather copy one of the versions installed on your system. That's why you first need to get a python2.6 binary for Ubuntu 14.04. It seems they don't officially support python2.6 anymore, so either you manually download and install it from http://python.org or use a ppa like this:
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.6
afterwards you can tell virtualenv to use python2.6 like this:
virtualenv -p python2.6 --no-site-packages /path/to/sandbox

apt-get installing older version of packages (Ubuntu)

I'm trying to install pip and virtualenv on a server (running Ubuntu 12.04.4 LTS) on which I have access, but I can only do it with sudo apt-get install (school politics). The problem is that althought I have run the sudo apt-get update command to update the packages list, I think it keeps installing old ones. After doing sudo apt-get install python-pip python-virtualenv, I do pip --version on which I get the 1.0, and virtualenv --version on which I get 1.7.1.2. These two version are quite old (pip is already in 1.5.5 and virtualenv in 1.11.5). I read that the problem is that the packages list is not up-to-date, but the command sudo apt-get update should solve this, but I guess no. How can I solve this? Thanks a lot!
apt-get update updates packages from Ubuntu package catalog, which has nothing to do with mainstream versions.
LTS in Ubuntu stands for Long Term Support. Which means that after a certain period in time they will only release security-related bugfixes to the packages. In general, major version of packages will not change inside of a major Ubuntu release, to make sure backwards-compatibility is kept.
So if then only thing you can do is apt-get update, you have 2 options:
find a PPA that provides fresher versions of packages that you need, add it and repeat the update/install exercise
find those packages elsewhere, download them in .deb format and install.
If you really need to use the latest stable versions of Python packages, then do not use apt-get for installing Python packages and use pip instead. If you would use apt-get and later install the same packages by means of pip or (better not) easy_install or setup.py, you are likely to run into version conflicts wondering, why your python based commands are of unexpected versions, or even worse, why they do not work at all.
I try to follow this pattern:
1. system wide pip installation first
Using instructions from here: http://pip.readthedocs.org/en/latest/installing.html find get-pip.py script, download it and run as python script.
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ rm get-pip.py
2. use pip to install virtualenv system wide
this shall be as easy as:
$ sudo pip install virtualenv
3. (optional) install virtualenvwrapper - system wide or to user profile
$ sudo pip install virtualenvwrapper
and follow instructions for configuring it.
4. Since now, install inside your virtualenv environments
This shall prevent conflicts between various versions of packages.
You are free to update particular virtualenvs as you need one by one independently.
5. (optional) Configure installation cache directories for installation speed
There are method how to speed up repeated installation of packages, what comes handy if you get used using virtualenv often. For details see my answer: https://stackoverflow.com/a/18520729/346478

Uninstall Django completely

I uninstalled django on my machine using pip uninstall Django. It says successfully uninstalled whereas when I see django version in python shell, it still gives the older version I installed.
To remove it from python path, I deleted the django folder under /usr/local/lib/python-2.7/dist-packages/.
However sudo pip search Django | more /^Django command still shows Django installed version. How do i completely remove it ?
pip search command does not show installed packages, but search packages in pypi.
Use pip freeze command and grep to see installed packages:
pip freeze | grep Django
Got it solved. I missed to delete the egg_info files of all previous Django versions. Removed them from /usr/local/lib/python2.7/dist-packages. Also from /usr/lib/python2.7/dist-packages (if any present here)
sudo pip freeze| grep Django
sudo pip show -f Django
sudo pip search Django | more +/^Django
All above commands should not show Django version to verify clean uninstallation.
Use Python shell to find out the path of Django:
>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.7/dist-packages/django/__init__.pyc'>
Then remove it manually:
sudo rm -rf /usr/local/lib/python2.7/dist-packages/django/
open the CMD and use this command :
**
pip uninstall django
**
it will easy uninstalled .
first use the command
pip uninstall django
then go to python/site-packages and from there delete the folders for django and its site packages, then install django. This worked for me.
Remove any old versions of Django
If you are upgrading your installation of Django from a previous version, you will need to uninstall the old Django version before installing the new version.
If you installed Django using pip or easy_install previously, installing with pip or easy_install again will automatically take care of the old version, so you don’t need to do it yourself.
If you previously installed Django using python setup.py install, uninstalling is as simple as deleting the django directory from your Python site-packages. To find the directory you need to remove, you can run the following at your shell prompt (not the interactive Python prompt):
$ python -c "import django; print(django.path)"
I had to use pip3 instead of pip in order to get the right versions for the right version of python (python 3.4 instead of python 2.x)
Check what you got install at:
/usr/local/lib/python3.4/dist-packages
Also, when you run python, you might have to write python3.4 instead of python in order to use the right version of python.
On Windows, I had this issue with static files cropping up under pydev/eclipse with python 2.7, due to an instance of django (1.8.7) that had been installed under cygwin. This caused a conflict between windows style paths and cygwin style paths. So, unfindable static files despite all the above fixes. I removed the extra distribution (so that all packages were installed by pip under windows) and this fixed the issue.
I used the same method mentioned by #S-T after the pip uninstall command. And even after that the I got the message that Django was already installed. So i deleted the 'Django-1.7.6.egg-info' folder from '/usr/lib/python2.7/dist-packages' and then it worked for me.
The Issue is with pip --version or python --version.
try solving issue with pip2.7 uninstall Django command
If you are not able to uninstall using the above command then for sure your pip2.7 version is not installed so you can follow the below steps:
1)which pip2.7
it should give you an output like this :
/usr/local/bin/pip2.7
2) If you have not got this output please install pip using following commands
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python2.7 get-pip.py
3) Now check your pip version : which pip2.7 Now you will get
/usr/local/bin/pip2.7 as output
4) uninstall Django using pip2.7 uninstall Django command.
Problem can also be related to Python version.
I had a similar problem, this is how I uninstalled Django.
Issue occurred because I had multiple python installed in my virtual environment.
$ ls
activate activate_this.py easy_install-3.4 pip2.7 python python3 wheel
activate.csh easy_install pip pip3 python2 python3.4
activate.fish easy_install-2.7 pip2 pip3.4 python2.7 python-config
Now when I tried to un-install using pip uninstall Django Django got uninstalled from python 2.7 but not from python 3.4 so I followed the following steps to resolve the issue :
1)alias python=/usr/bin/python3
2) Now check your python version using python -V command
3) If you have switched to your required python version now you can simply uninstall Django using pip3 uninstall Django command
Hope this answer helps.
If installed Django using python setup.py install
python -c "import sys; sys.path = sys.path[1:]; import django; print(django.__path__)"
find the directory you need to remove, delete it

Categories