Skip to content

Instantly share code, notes, and snippets.

@anhtranbk
Last active June 18, 2018 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhtranbk/9aee20510d85a700495ad17fd2c33529 to your computer and use it in GitHub Desktop.
Save anhtranbk/9aee20510d85a700495ad17fd2c33529 to your computer and use it in GitHub Desktop.
Python notes

Lỗi:

Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

khi gọi: sudo pip install trên Ubuntu sau khi update pip lên bản 10.x.

Lí do

Do bản update mới của pip được cài trong thư mục /home/user/.local nên khi gọi pip không qua sudo thì sẽ không gây lỗi vì file pip mới đã được sửa đổi để phù hợp cho bản 10.x. Tuy nhiên khi gọi pip thông qua sudo thì hệ thống sẽ sử dụng file pip nằm trong /usr/bin/pip, trong đó câu lệnh from pip import main không hoạt động trên pip 10.x

Cách sửa

Install pip vào thư mục local (không sử dụng sudo): pip instal --user package hoặc sửa file /usr/bin/pip thành như sau:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

Python 2

sudo apt-get install python-pip
# pip install packages into /usr/local so it needs sudo permission
sudo pip install virtualenv
# force pip to install packages into user local directory at /home/<user>/.local
pip install --user virtualenv

virtualenv venv
virtualenv -p /usr/bin/python2.7 ven3

Python3

sudo apt-get install python3-pip
# pip3 automatically install packages into user local directory at /home/<user>/.local
pip install virtualenv

virtualenv venv3
virtualenv -p python3 venv3
python3 -m venv venv3

If virtualenv exists at both /usr/local and /home//.local, virtualenv at user local directory will have higher priority and will be used as default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment