Can't Import Django using Python3 on macOS Sierra - python

Have both versions of Python (Legacy 2.7.10 and 3.6.2) installed on macOS Sierra.
Installed pip using the following steps.
Downloaded it using curl:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Installed pip it by running the installer script:
sudo python get-pip.py
Checked for any upgrades / updates for pip:
sudo pip install -U pip
Installed django 1.11:
sudo pip install django==1.11
When I run python (legacy):
python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.11
However, when trying it using python3:
python3
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
Note: I followed a different tutorial and got python3 working with django 1.11 using virtualenv - please don't suggest this as I am new to the python world and just want to use python3 / django 1.11 in a non-virtual environment - I just want have it working like the legacy python interpreter is behaving.

pip installs libraries differently for python2 and python3, so you effectively have different environments for each. If you want to install Django for python3, you'll want to install it like this:
pip3 install django==1.11

Related

"python" command can't find Python2 installation

I have screwed up my python install on my MacOS machine.
Here it is -
REMs-MBP:opt rem$ which python
/usr/bin/python
REMs-MBP:opt rem$ python
-bash: /usr/local/opt/python/libexec/bin/python: No such file or directory
REMs-MBP:opt rem$ which python2
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
REMs-MBP:opt rem$ python2
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 12:01:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
python2 works fine but default python is not working fine.
How can I fix it?
brew install python by default tries to install python3. I need python2.7 as my default.
Updates:
pip still pointing to python3 installation.
PREMs-MBP:opt prem$ which python
/usr/bin/python
PREMs-MBP:opt prem$ pip
-bash: /usr/local/bin/pip: /usr/local/Cellar/python/3.7.0/bin/python3.7: bad interpreter: No such file or directory

Cannot import any modules (installed used pip) in python 2.7.14

I downloaded the source code of python 2.7.14 and built it and installed it on linux ( Red Hat 4.8.5-16 ). I have earlier installed python-magic and requests libraries. Now when I try to import modules installed using pip, I get this:
$ python2
Python 2.7.14 (default, Nov 9 2017, 09:05:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named magic
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
while similar thing works perfectly fine in python 2.7.5 (default with the RHEL system)
$ python
Python 2.7.5 (default, May 3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> import requests
>>>
Am I missing any configuration step here?
The modules installed via pip are only available to the standard python version. You need to install your desired packages for the non-standard python versions as well (see also Installing Python Modules)
python2 -m pip install python-magic
python2 -m pip install requests
your pip is default set for version
$ python
Python 2.7.5
SO if you want to install module for
$ python2
Python 2.7.14
use python2 -m pip install module_name
The sys.path must be different for the two installations. That's a list of folders that Python checks for imports.
It's probably simplest to just symlink the one for wherever pip installs to into the modules folder for 2.7.14.

Upgrading devel version of statsmodels

I've got statsmodels 0.5.0 and I want to upgrade to the latest version which is 0.6.0.
So I do
$ git clone git://github.com/statsmodels/statsmodels.git
$ cd statsmodels
$ pip install .
But, I get an error:
error: can't copy 'statsmodels/nonparametric/_smoothers_lowess.c': doesn't exist or not a regular file
Apparently the solution is to install Cython (which I thought was already installed, or how could statsmodels 0.5.0 ever have worked in the first place..?!?).
Anyway, so it becomes:
$ git clone git://github.com/statsmodels/statsmodels.git
$ cd statsmodels
$ pip install cython
$ pip install .
$ python
Python 2.7.3 |CUSTOM| (default, Apr 11 2012, 17:52:16)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsmodels
>>> statsmodels.version.full_version
'0.6.0.dev-b472807'

How to get pip to point to newer version of Python

I have two versions of Python installed on my centOS server.
[ethan#demo ~]$ python2.6 --version
Python 2.6.6
[ehtan#demo ~]$ python --version
Python 2.7.3
The older version (2.6) is required by some essential centOS packages so I can't remove it.
When I install packages using pip, they are being installed in Python 2.6. But instead I want them to be installed to Python 2.7.
How can I change this behaviour?
For example, here is what happened when I tried installing Wand
[ethan#demo ~]$ pip install Wand
Requirement already satisfied (use --upgrade to upgrade): Wand in /usr/lib/python2.6/site-packages
Cleaning up...
[ethan#demo ~]$ python2.6
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wand
>>> exit()
[ethan#demo ~]$ python
Python 2.7.3 (default, Oct 11 2013, 15:59:28)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wand
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named wand
>>> exit()
Edit
I found this answer but it didn't work for me https://stackoverflow.com/a/4910393/3384340
You need to install pip for each python version separately.
In order to install pip for Python2.7, run
sudo easy_install-2.7 pip
Use pip-2.7 to install Wand
sudo pip-2.7 install Wand

Mac OSX: Error after installing Django - No module named django

I am using MacOS X 10.7.5 and am a TOTAL newb at Python
I am pretty sure django installed correctly as per https://code.djangoproject.com/wiki/Distributions
$ sudo port install py27-django
---> Computing dependencies for py27-django
---> Cleaning py27-django
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
The issue was with that a prior Python was installed (2.7.1) previously and MacPorts installed a new one (2.7.6) but did not update the Mac Sym links etc
So I had to futz with my PATH
$ vi ~/.bash_profile
.
.
export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH
.
.
$ source ~/.bash_profile
$ python --version
Python 2.7.6
$ python
Python 2.7.6 (default, Nov 12 2013, 13:12:10)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.get_version()
1.5.1

Categories