I am trying to access a python file created on PyCharm with my terminal. When I import modules on PyCharm they are found and successfully imported (e.g tkinter, PIL etc.). When I try to run the file on my terminal I get the following message :
Traceback (most recent call last):
File "THE_project_GUI.py", line 3, in <module>
from PIL import Image
ImportError: No module named PIL
The which python command returns : /usr/bin/python
The which -a pip command returns : /usr/local/bin/pip
How do make sure I use the interpreter that pip is installing for?
Trial:
tromgy's solution:
me#mahmouds-mbp-2 ~ % ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
me#mahmouds-mbp-2 ~ % cd /Users/me/Documents/Programming/THE_project
me#mahmouds-mbp-2 THE_project % /usr/local/bin/python3.8 /usr/local/bin/python THE_project_GUI.py
returns:
File "/usr/local/bin/python", line 1
SyntaxError: Non-UTF-8 code starting with '\xcf' in file /usr/local/bin/python on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
v0idbar's solution:
I have installed python 3.7 using :
brew install python#3.7
returns :
Python has been installed as
/usr/local/opt/python#3.7/bin/python3
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been installed into
/usr/local/opt/python#3.7/libexec/bin
You can install Python packages with
/usr/local/opt/python#3.7/bin/pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
I have changed the Pycharm interpreter by going to File> New project settings> Preferences for new projects. And then added 3.7 after finding it using /usr/local/opt/python#3.7/bin/python3.
Then I ran the following :
me#mahmouds-mbp-2 THE_project % /usr/local/opt/python#3.7/bin/python3
/usr/local/opt/python#3.7/bin/python3
Python 3.7.11 (default, Jul 6 2021, 12:43:19)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> /usr/local/opt/python#3.7/bin/python3 THE_project_GUI.py
File "<stdin>", line 1
/usr/local/opt/python#3.7/bin/python3 THE_project_GUI.py
^
SyntaxError: invalid syntax
Try run pip -V to figure out what python version it is using:
$ pip -V
pip 19.0.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
You can check if it the same version as the python executable you are using: python -m pip -V
$ python -m pip -V
pip 9.0.1 from /Users/lev/anaconda2/lib/python2.7/site-packages (python 2.7)
You probably have a mismatch between those versions.
Related
I recently added python 3.9 to my ubuntu installation.
Can run python3.9 code using an installed package from bash, can also run python 3.9 using an installed module from a bash script using a shebang to load the python 3.9 environment, fairly standard stuff.
But when I try running an installed script from https://github.com/gitbls/imon, the installed package is not found.
initially I thought this was fixed by changing the imon bash script shebang to reference the new python.
ie
old
#!/usr/bin/python3
new
#!/usr/bin/python3.9
also added these lines to the imon script to verify which version of python in environment
import sys
print("sys.version:", sys.version)
when I run the /usr/local/bin/imon package, it reports 'no module' error.
$ sudo /usr/local/bin/imon --nosyslog --instance my_instance_name
sys.version: 3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
Traceback (most recent call last):
File "/usr/local/bin/imon", line 11, in <module>
from icmplib import ping, multiping
ModuleNotFoundError: No module named 'icmplib'
the thing that is really throwing me, is the output shows the imon script is accepting the shebang to load python 3.9, but will not find the module, despite my script using identical code able to load the module.
my test exercises below to verify and demonstrate I have python3.9 installed and a bash script can load python3.9 and the icmplib module.
$ python3.9
Python 3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print (sys.version)
3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
>>> import icmplib
ie: the icmplib package has been installed and can be used when in the python shell.
I created a shell script and made it executable
vim test_script.sh
#!/usr/bin/python3.9
import sys
print (sys.version)
import icmplib
print("icmplib.__version__:", icmplib.__version__)
chmod u+x test_script.sh
output from this shows the script is running python3.9 and can access the installed module.
$ ./test_script.sh
3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
icmplib.__version__: 3.0.3
#EDIT START--------------------------------------------------------------
added tests as suggested by #furas in comments.
print("sys.path:", sys.path)
print("icmplib.__file__:", icmplib.__file__)
these above lines were added to both test_script.sh & /usr/local/bin/imon
the results below reveal the local test_script.sh accesses a directory not accessed by the imon script run by root.
'/home/m/.local/lib/python3.9/site-packages',
oddly, both the script run locally and the script run under root access path
'/usr/lib/python3/dist-packages'
(I don't know why python3 instead of python3.9 is in sys.path, this seems odd to me)
taking another suggestion from #furas I tried pip installing the module as root
$ sudo python3.9 -m pip install icmplib
Collecting icmplib
Using cached icmplib-3.0.3-py3-none-any.whl (30 kB)
Installing collected packages: icmplib
Successfully installed icmplib-3.0.3
this looked helpful. unfortunately running the imon script as root still resulted in the Namerror.
$ sudo /usr/local/bin/imon --nosyslog --instance imon-bmt1
sys.version: 3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
sys.path: ['/usr/local/bin', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
File "/usr/local/bin/imon", line 8, in <module>
print("icmplib.__file__:", icmplib.__file__)
NameError: name 'icmplib' is not defined
I am now 18hrs awake and brain fading. :(
#EDIT END--------------------------------------------------------------
#EDIT: this below was before the edit above.
when I run test_script.sh as root, I can replicate the error experienced by imon.
$ ./test_script.sh
3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
icmplib.__version__: 3.0.3
sys.path: ['/home/m/f_projs/internet_monitor',
'/usr/lib/python39.zip',
'/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload',
'/home/m/.local/lib/python3.9/site-packages',
'/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']
icmplib.__file__: /home/m/.local/lib/python3.9/site-packages/icmplib/__init__.py
$ sudo /usr/local/bin/imon --nosyslog --instance imon-bmt1
sys.version: 3.9.14 (main, Sep 7 2022, 23:43:29)
[GCC 9.4.0]
sys.path: ['/usr/local/bin',
'/usr/lib/python39.zip',
'/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload',
'/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
File "/usr/local/bin/imon", line 8, in <module>
print("icmplib.__file__:", icmplib.__file__)
NameError: name 'icmplib' is not defined
my environment = ubuntu 20.04 with python 3.9.14
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
$ which python3
/usr/bin/python3
$ which python3.9
/usr/bin/python3.9
$ python3.9 --version
Python 3.9.14
$ python3 --version
Python 3.8.10
I'm marking this as 'answered' because I've realised I broke my python3 and python3.9 install beyond repair while attempting to fix this issue.
now I have a completely different problem.
most of the original problem appears to be failing to use 'sudo pip install X' (or 'sudo python3.9 -m pip install x') when installing packages. I normally use virtual env for my local projects.
I was doing uninstall of python3.9 and cleanup with view to reinstalling python3.9.
sudo apt remove python3.9
#now check what packages remain to be removed manually.
dpkg --list | grep python3.9
#this showed a short list of packages to be cleaned up.
#nb: 'rc' = package has been removed, but configuration files remain
sudo apt-get remove --auto-remove python3.9-minimal
sudo apt-get remove --auto-remove python3.9-venv
sudo apt-get remove --auto-remove python3.9
sudo dpkg -P libpython3.9-minimal
sudo dpkg -P python3.9-minimal
sudo dpkg -P python3.9-venv
#then manually deleting the python3.9 directories previously listed by sys.path
ie
sudo rm -rf /usr/lib/python3.9
sudo rm -rf /usr/local/lib/python3.9/dist-packages
my new problem is due to accidentally deleting a directory from sys-path which 'apt-get remove' and 'sudo dpkg -P' didn't cleanup.
sudo rm -rf /usr/lib/python3/dist-packages
hindsight screams out this will destroy the python3 install. Bitter experience just now tells me it is near impossible to repair since the apt-get tools are dependent on a working python3 install.
I think the only way to fix this is copying across a full directory of '/usr/lib/python3/dist-packages' from a clean install.
new SO question for my new problem > (how to repair python3 install on ubuntu after rm -rf /usr/lib/python3/dist-packages?)
Hopefully this post will be useful and a warning to others in future.
I followed this Medium article to upgrade Python 3.5 (to be specific, Python 3.5.2) to 3.6 (to be specific, Python 3.6.3) in my Ubuntu 16.04 LTS.
In that article, I had skipped the first step which is ...
Login via SSH and update all installed packages
First of all, login to your Ubuntu 16.04 VPS via SSH as user root
ssh root#IP_Address -p Port_number
and update all installed packages
I followed the remaining steps and between step 4 (Method 1) & step 5 (Method 2), I had chosen step 4 (Method 1). During all the steps, I didn't get even a single error. Now, in general, I'm not having any side-effects in my machine but from a Python perspective, I'm having the following issues:
Python 3.5.2 is still there in my machine: if I type Python3 then 3.6.3 gets opened but if I type Python3.5 then 3.5.2 gets opened up. So, even after upgrading, is it normal to have Python 3.5.2 in my machine?
I can't install any Python package/library using pip3 because it still points to Python 3.5.2 only.
When I open Jupyter notebook, it's still pointing to Python 3.5.2 (I'm not using virtualenv) How can I point it to Python 3.6.3?
Additional Info:
Here is the output of some commands:
$ python -V
Python 2.7.12
$ python3 -V
Python 3.6.3
$ python3.5 -V
Python 3.5.2
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
$ which python3.5
/usr/bin/python3.5
When I try to import some libraries using Python3.5 (pointing to 3.5.2) v/s Python3 (pointing to 3.6.3)
$ python3.5
Python 3.5.2 (default, Jul 17 2020, 14:04:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
$ python3
Python 3.6.3 (default, Jul 15 2020, 20:42:43)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
pip versions:
$ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
$ pip -V
pip 20.1.1 from /home/milan/.local/lib/python3.5/site-packages/pip (python 3.5)
$ pip3 -V
pip 20.1.1 from /home/milan/.local/lib/python3.5/site-packages/pip (python 3.5)
So, running pip3 install numpy installs numpy for Python 3.5.2 not for Python 3.6.3
As typing Python3 open Python 3.6.3, so I even tried $ python3 -m pip install numpy command to install package e.g. numpy for my Python 3.6.3 but got the following error(s):
Collecting numpy
Downloading https://files.pythonhosted.org/packages/22/e7/4b2bdddb99f5f631d8c1de259897c2b7d65dcfcc1e0a6fd17a7f62923500/numpy-1.19.1-cp36-cp36m-manylinux1_x86_64.whl (13.4MB)
100% |████████████████████████████████| 13.4MB 124kB/s
Installing collected packages: numpy
Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/local/lib/python3.6/site-packages/pip/commands/install.py", line 342, in run
prefix=options.prefix_path,
File "/usr/local/lib/python3.6/site-packages/pip/req/req_set.py", line 784, in install
**kwargs
File "/usr/local/lib/python3.6/site-packages/pip/req/req_install.py", line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/usr/local/lib/python3.6/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
isolated=self.isolated,
File "/usr/local/lib/python3.6/site-packages/pip/wheel.py", line 345, in move_wheel_files
clobber(source, lib_dir, True)
File "/usr/local/lib/python3.6/site-packages/pip/wheel.py", line 316, in clobber
ensure_dir(destdir)
File "/usr/local/lib/python3.6/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
os.makedirs(path)
File "/usr/local/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/numpy'
You are using pip version 9.0.1, however version 20.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
So, now, how can I install packages/libraries for Python 3.6.3 in my Ubuntu 16.04 LTS? Is it even possible?
How can I point my Jupyter Notebook to point to Python 3.6.3 without installing virtualenv? Currently, here is the output from my Jupyter Notebook which is very confusing:
1. from platform import python_version
2. print(python_version())
3.5.2
1. !python3 -V
Python 3.6.3
Also, after seeing the above outputs for different commands, is my system messed up now (I mean from a system point of view)? Do I have to reinstall my Ubuntu 16.04?
After updating binaries you should run to refresh your cache with the newest versions of your binaries (python3.6 instead of python3.5!)
sudo ldconfig
After that, run
python3 -m pip install --upgrade pip
to ensure correct pip version for your new python binary.
No, your system is not messed up and you do NOT have to reinstall Ubuntu.
greetings
I have opencv installed on my mac which was working a few months ago but doesn't work anymore.
I have the following specificities.
- OpenCV: 3.4.3.1
- Operating System / Platform: macOS 10.14
- Python 3.6.5
I tried to reload a python script where I used opencv. However, I got the following error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/cv2.so, 2): Library not loaded: /usr/local/opt/ilmbase/lib/libImath-2_2.12.dylib
Referenced from: /usr/local/Cellar/opencv/3.4.1_4/lib/libopencv_imgcodecs.3.4.dylib
Reason: image not found
I then tried to re-install opencv with the step by step explanation of this site with which I successfully installed opencv for the first time.
determine which version of opencv I have on my machine
After having written cd /usr/local/Cellar/opencv and ls I get: 3.4.1_4 3.4.3 3.4.3_1. So I conclude that the latest version on my machine is 3.4.3_1 which is in /usr/local/Cellar/opencv/3.4.3_1/cv2.cpython-37m-darwin.so
Determine the path for package in python
Then if i do the following $ cd /usr/local/Cellar/opencv/3.4.3_1 and $ cd lib, I get (among others): python3.7.
Then I do the following:
$ cd python3.7 and then $ cd site-packages and $ pwd, I get: /usr/local/Cellar/opencv/3.4.3_1/lib/python3.7/site-packages
add the cv2..so to System python
I enter then python with $ python3 and get
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Question N°1 Isn't it weird that it says version 3.6.5 and opencv seems to have a lib 3.7 (and no 3.6)
type >>> import sys and then >>> print(sys.path)and get the following list:
['', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
I then enter:
$ ln -s /usr/local/Cellar/opencv/3.4.3_1/lib/python3.7/site-packages/cv2.cpython-37m-darwin.so /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cv2.so
Add OpenCV to Virtualenvs
I then add the following command:
$ mkdir ~/Dev
$ cd ~/Dev
$ virtualenv -p python3 newcvtest
$ cd newcvtest
$ source bin/activate
(newcvtest) $ python --version
Python 3.6.5
$ pip install numpy
$ cd lib/python3.6/site-packages
$ ln -s /usr/local/Cellar/opencv/3.4.3_1/lib/python3.7/site-packages/cv2.cpython-37m-darwin.so
After that the installation should be finished. However I still get the same error message
What should I do?
Actually opencv was successfully installed.
I had actually to change the Path in the .json file of the jupyter kernel. First I checked the list of jupyter kernel:
$ jupyter kernelspec list
julia-0.6 /Users/mymac/Library/Jupyter/kernels/julia-0.6
julia-1.0 /Users/mymac/Library/Jupyter/kernels/julia-1.0
python3 /Users/mymac/Library/Jupyter/kernels/python3
I then cd the above python path and, I found the file kernel.json inside and opened it:
{
"argv": [
"/path/to/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
In that file, I then changed the line /path/to/python by the python path I got when typing the following in Terminal:
$ which python
/Users/mymac/anaconda3/bin/python
Relaunched Atom and it finally worked!
The hints of the github page of jupyter helped also a lot!
I am using Ubuntu 16 machine. I want to use python 3. I isntalled it. However, I have to use the command python3 otherwise it runs python2.7.
I installed pycrypto library using pip install pycrypto but when I try to import from pycrypto using python3 I get this error:
>>> from Crypto.Cipher import AES
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'Crypto'
While I do not face the same problem in python 2.7 (the import works fine). What is the problem? how to solve it?
UPDATE:
I tried pip3 and this is the result:
x#x-VirtualBox:~$ sudo -H pip3 install pycrypto
Requirement already satisfied: pycrypto in /usr/local/lib/python3.6/dist-packages (2.6.1)
x#x-VirtualBox:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import AES
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'Crypto'
>>>
Apparently, you have 2 Python3 instances installed:
Python3.6:
Which is the one you want to use
Where pycrypto is installed (by pip3)
Python3.5.2:
Which is launched by python3 command
So, you are launching the wrong Python interpreter, most likely because python3 points to /usr/bin/python3 (you can check that by typing which python3 in your shell), which is Python3.5.2, and whose path is in the ${PATH} env var.
From your pip paths, it seems like Python3.6 is installed under /usr/local (and the executable would be /usr/local/bin/python3), so you can either:
Launch the Python3 executable by its full path (/usr/local/bin/python3, as stated above)
Add /usr/local/bin to ${PATH} before /usr/bin, and then simply launch Python3.6 by typing python3 in your shell - but I'd advise against that
There are other methods (e.g. creating an alias), but I guess you got the idea
#EDIT0:
Some more info as requested in comments. This has nothing to do with Python, it' all just Ubtu stuff:
To list packages: use apt or dpkg
To check Python2.7 (or any other version): use which (as above)
You don't need to uninstall Python3.5. Multiple version can coexist safely
If you want to make one as default, make an alias (like I did at the end of the example below) and if you want it to be persistent, place it in your profile file (e.g. .profile, .bashrc, .bash_profile)
Examples (on my VM):
[cfati#cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050526408]> apt list python python3
Listing... Done
python/xenial-updates,now 2.7.12-1~16.04 amd64 [installed]
python3/xenial,now 3.5.1-3 amd64 [installed]
[cfati#cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050526408]> which python
/usr/bin/python
[cfati#cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050526408]> ll /usr/bin/python
lrwxrwxrwx 1 root root 7 mar 12 16:25 /usr/bin/python -> python2*
[cfati#cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050526408]> dpkg -S /usr/bin/python3 /usr/bin/python2
python3-minimal: /usr/bin/python3
python-minimal: /usr/bin/python2
[cfati#cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050526408]> alias python3=/usr/local/bin/python3
As you are using python3:
pip3 install pycrypto
This all began when I set out to install the Requests library for Python 3 (I'm running on OSX Mavericks with Python 2.7.5 (installed by brew install python) and 3.4.2 (installed by brew install python3). When I run pip3 --version (or anything related to the pip3 command) I see this:
$ pip3 --version
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/usr/local/lib/python3.4/site-packages/pip/__init__.py", line 11, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/local/lib/python3.4/site-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download import path_to_url
File "/usr/local/lib/python3.4/site-packages/pip/download.py", line 22, in <module>
from pip._vendor import requests, six
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", line 49, in <module>
from ndg.httpsclient.ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ndg/httpsclient/ssl_peer_verification.py", line 17
except ImportError, e:
^
SyntaxError: invalid syntax
When I run the Python 2.7.5 version I see this:
$ pip --version
pip 1.5.6 from /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg (python 2.7)
Just for sanity purposes Here is what when I see when I enter the interactive interpreters:
$ python3
Python 3.4.2 (default, Oct 19 2014, 17:52:17)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
A lot of the other answers related to updating pip3 suggest that I update pip3 with this commend pip3 install --upgrade pip which gives the same error, or I use easy_install -U pip but because of how brew sets up the Pythons, it only updates the Python 2.7.5 version (there is no easy_install3). Any ideas?
The root problem is that you somehow got a Python 2.x-only package installed into your 3.x site-packages.
Underlying that, you've actually got two different Python 2.7 installations (Apple's and Homebrew's) crossed with each other, which may have something to do with how you got a 2.7 package into 3.x as well…
Anyway, the reason this is breaking pip is that pip has various optional dependencies that it tries to import if present, and some of them do the same, and so on, and ultimately, starting up pip is importing the ndg-httpsclient package.
I'm not sure how you got that package. A standard Homebrew 3.x looks in two extra site-packages directories (fire up python3 then import sys; print(sys.path) to see all of the places it looks, both stdlib and site) beyond the one that pip3 installs into.
In this case, you've somehow installed the 2.x version of ndg-httpsclient into /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages.
Since you didn't install it with pip—and, more to the point, since you can't run pip in the first place—you can't just pip uninstall it. So:
rm -rf /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ndg*
This could break some other packages that depend on it. Once you get things working, you can use pip3 list to see all the site packages you've installed and test them out. If you want to be paranoid, do something like this:
$ pip3 list > mypackages
$ rm -rf <each site-package directory>
$ brew uninstall python3
$ brew install python3
$ pip3 install -r mypackages
You might want to similarly clean up your Homebrew 2.7 (or just scrap it and only use Apple's—especially since I'm pretty sure you're running Apple's anyway), and the site-packages for the Apple 2.7 (but not Apple's Python itself, of course, because you can't uninstall that).