25

My ultimate goal is to download and install awscli http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-linux.html

Seems python and pip are required in order to accomplish my goal.

Installing python via yum isn't working for me, so I downloaded and installed python3 manually as follows:

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
tar xf Python-3.6.1.tar.xz
cd Python-3.6.1.tar.xz
./configure
make
sudo make altinstall

When I do

pip3.6 --version

I get this:

Traceback (most recent call last):
  File "/usr/local/bin/pip3.6", line 7, in <module>
    from pip import main
ImportError: cannot import name 'main'

Environment: RHEL 6.6 Accessed via ssh

What is going on? How to install pip and python3.6 correctly on my linux box?

Edit:

When trying to install via yum it fails so I am trying to avoid using yum:

[asemani@rcdc9pfda5r Python-3.6.1]$ sudo yum -y install python-pip

[sudo] password for asemeani: 
Loaded plugins: security
Setting up Install Process
No package python-pip available.
Error: Nothing to do

Another Edit:

After importing sys and then called sys.path this is what I get:

[asemani@rcdc9pfda5r Python-3.6.1]$ python3.6
Python 3.6.1 (default, Apr 28 2017, 11:03:27) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
>>> quit()
[asemani@rcdc9pfda5r Python-3.6.1]$ ls -l /usr/local/lib/python3.6/site-packages
total 32
-rw-------  1 root root  126 Apr 27 23:45 easy_install.py
drwx------ 11 root root 4096 Apr 27 23:45 pip
drwx------  2 root root 4096 Apr 27 23:45 pip-9.0.1.dist-info
drwx------  5 root root 4096 Apr 27 23:45 pkg_resources
drwx------  2 root root 4096 Apr 27 23:45 __pycache__
-rw-r--r--  1 root root  119 Apr 27 23:45 README.txt
drwx------  5 root root 4096 Apr 27 23:45 setuptools
drwx------  2 root root 4096 Apr 27 23:45 setuptools-28.8.0.dist-info
Moonie Asemani
  • 375
  • 1
  • 4
  • 13
  • "*Installing python via yum isn't working for me*" - how is it failing? – melpomene Apr 28 '17 at 08:04
  • Not quit sure but it doesn't seem to find the python or pip package when yumming. I will update my question to include detail – Moonie Asemani Apr 28 '17 at 08:07
  • This looks relevant: https://packaging.python.org/install_requirements_linux/ – melpomene Apr 28 '17 at 08:11
  • Remove it then install again. `yum remove python-pip` then `yum install python-pip` – Chankey Pathak Apr 28 '17 at 08:12
  • @melpomene I was looking at the document earlier today. As mentioned yum doesn't seem to be working – Moonie Asemani Apr 28 '17 at 08:16
  • 1
    I believe you manually installed Python 3.6 correctly. You should then have a `python3` and `pip3` commands available from your terminal. What does `sys.path` contain after `import sys` using `python3`? You should have something like /somepath/python3.6/site-packages in it. Then what does `ls -l /somepath/python3.6/site-packages | grep pip` prints out? – Q Caron Apr 28 '17 at 08:59
  • @QCaron I've edited my question to answer your questions. However I'm still unclear what my problem is? – Moonie Asemani Apr 28 '17 at 18:17
  • ImportError: cannot import name 'main' Faced the same issue once I upgraded it to Python version 10. Got solution in -- https://github.com/pypa/pip/issues/5240 Simply restarting the terminal worked for me. – saurabh rathor Apr 18 '18 at 08:20
  • Check my answer, it should work for you. – Mulperi Apr 26 '18 at 17:03

10 Answers10

72

You need to edit the pip3 file like so (your path could be different):

nano /Library/Frameworks/Python.framework/Versions/3.6/bin/pip3

And change the import main line so it says:

from pip._internal import main

After this change, my pip3 started to work and I am on macOS. If you are using Linux, your path could be something like /usr/bin/pip3

I found this tip and more information from this thread: https://github.com/pypa/pip/issues/5240

Mulperi
  • 1,450
  • 1
  • 14
  • 24
17

Don't invoke pip/pip3 directly, which are shortcuts at different locations for different user and they are not well maintained when you upgrade pip (check 'which pip' and 'sudo which pip').

 $ sudo python -m pip install xxx  #for python2 
 $ sudo python3 -m pip install xxx  #for python3

These commands do not take shortcuts and directly invoke pip module, which is maintained very well (also works on Windows)

TingQian LI
  • 660
  • 8
  • 13
17

My OS is Linux Mint 18

sudo nano /usr/bin/pip3

Change

from pip import main

To

from pip._internal import main
Aftabul Islam
  • 454
  • 4
  • 10
6

sudo chmod -R a+rx /usr/local/lib/python3.6/site-packages

You can see the problem right there in your ls -l /usr/local/lib/python3.6/site-packages output that your pip directory is only readable by the owner, which is root.

blahblahetcetc
  • 191
  • 1
  • 6
Arthur
  • 61
  • 1
  • 3
5

Do a cd /usr/bin

Then sudo nano pip so as to edit the pip file

Change from pip import main to from pip._internal import main

This will resolve the issue

pt12lol
  • 2,332
  • 1
  • 22
  • 48
Iyanuoluwa Ajao
  • 655
  • 1
  • 7
  • 13
3

try the following as a way around the issue until it got solved

sudo python -m pip --version
>> pip 10.0.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
sudo python -m pip install numpy --upgrade
>> Requirement already up-to-date: numpy in /usr/local/lib/python2.7/site-packages (1.14.2)
Cobry
  • 4,348
  • 8
  • 33
  • 49
1

I don't exactly know the answer, but: that error indicates that the script can find some package called pip, but not the right one. Probably, an old version of pip, from back before they created a main method (you can check pip.__version__ from the python shell).

I'm willing to bet that you still have another, older version of python installed which has its own version of pip. For some reason your pythonpath is loading that one instead of the one that goes with py3.6.

Some possibilities to look into:

  • I don't know anything about redhat, but is there some redhat-specific way of choosing the "default python" to be used?

  • Is the shebang line at the top of the pip script something like #!/usr/bin/env python instead of #!/usr/bin/python3.6 like it should be?

  • Is it possible to modify your shell's PATH so that the downloaded python is used?

  • Is it possible to change your PYTHONPATH (i think it gets added to the default value of sys.path inside python; look it up) so that it loads the new pip instead of the old pip?

user3113723
  • 169
  • 13
  • 1
    Python 2.x is still the default Python version for many Linux / Unix based operating systems. Be careful with "choosing the default python to be used". – Q Caron Apr 28 '17 at 08:49
1

My OS was Mac sierra and I had to change the following line

from pip import main

into

from pip._internal import main

Kapidis
  • 129
  • 2
  • 7
0

Seems the get pip script was missing the fact that more than 1 version of python could reside on a machine. So I added the following lines:

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

For some reason it was missing this. Anyway this worked for me. Thanks for your help guys.

Moonie Asemani
  • 375
  • 1
  • 4
  • 13
0

Do the following steps to solve the problem --(Ubuntu-Debian)

step 1 - Go to directory

cd usr/bin/

and open pip3 file using gedit or nano(via terminal) .

Step 2- change from pip import main into from pip._internal import main

P.S - If you donot have permission to change use command - sudo chmod 777 pip3 and save this file.

Hope this helps!!