centos在python3環境下安裝PIP的問題?

centos6.6 python3.4.3 谷歌百度未找到解決方案

錯誤如下:

python setup.py install

Traceback (most recent call last):

File "setup.py", line 6, in &

from setuptools import setup, find_packages

File "/usr/local/lib/python3.4/site-packages/setuptools/__init__.py", line 11, in &

from setuptools.extension import Extension

File "/usr/local/lib/python3.4/site-packages/setuptools/extension.py", line 8, in &

from .dist import _get_unpatched

File "/usr/local/lib/python3.4/site-packages/setuptools/dist.py", line 18, in &

from setuptools import windows_support

File "/usr/local/lib/python3.4/site-packages/setuptools/windows_support.py", line 2, in &

import ctypes

File "/usr/local/lib/python3.4/ctypes/__init__.py", line 7, in &

from _ctypes import Union, Structure, Array

ImportError: No module named "_ctypes"


昨天遇到這個問題,也是搜了一天未找到辦法,最後還是看源碼解決的。

源碼編譯安裝Python3(.4.4),是自帶且默認安裝pip、setuptools的,如果裝完python發現沒有裝這倆庫,說明你的安裝環境不全。

手機碼字太累,先補個覺一會上電腦答。

============2016-5-24 09:32:01Update===========

編譯安裝python是需要的zlib.h/openssl/h等文件可以簡單安裝:

yum install zlib-devel openssl-devel

這麼裝完再重新編譯安裝python3就可以了~~~

========2016-5-2 11:11:16Update=========

我回來了。。補充下答案。。。

環境:裸CentOS 7.2,自帶python2.7.5

  1. 下載https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz

  2. tar解壓,再按照README裡面的說明安裝,

./configure
make
make altinstall

3、安完發現沒有pip3,全局搜索

find / -name "pip*"

只有

/usr/bin/pip

/usr/bin/pip2

/usr/bin/pip2.7

4、去官網看看

說明3.4已經自帶了,那為什麼我安完了沒有?

官網也提供了pip安裝腳本https://bootstrap.pypa.io/get-pip.py

wget 下來,用指定的版本安裝試試

python3.4 get-pip.py

與@老槐楊 一樣的錯誤

zlib not available

Package http://zlib-1.xxx already installed

明明已經安裝了呀?怎麼還是不行?

# python3.4
Python 3.4.4 (default, May 2 2016, 08:28:47)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
&>&>&> import zlib
Traceback (most recent call last):
File "&", line 1, in &
ImportError: No module named "zlib"

握草,沒有zlib這個庫?試了下python2.7,發現可以呀!

yum install zlib跟import zlib 的zlib好像不是一個東西哦!

百度谷歌搜了一圈也沒找到答案,但是,你有源代碼啊!為何不看看你敲的代碼到底在做什麼。於是我們重新編譯Python3.4.4,看看到底發生了什麼~

在make的結果里發現了點端倪

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_sqlite3 _ssl _tkinter
readline zlib
To find the necessary bits, look in setup.py in detect_modules() for the module"s name.

模塊的二進位文件沒找到,具體需要哪些文件,可以到setup.py里看一看。

好吧......

vim setup.py

很容易找到這麼一句

zlib_inc = find_file("zlib.h", [], inc_dirs)
if zlib_inc is not None:
......
else
missing.append("zlib")

都是有源碼的,順藤摸瓜看!在inc_dirs路徑里找zlib.h文件?inc_dirs是哪些路徑,當然也可以往上跟蹤查到(實在不行還可以print出來),然後,再一次~全局搜索

find / -name "zlib.h"

根本木有這個文件!!!

else 分句把缺失zlib列印出來了,不是yum顯示已經安裝了zlib嗎,輸入

# zip
Copyright (c) 1990-2008 Info-ZIP - Type "zip "-L"" for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

確實已經安了,但是並沒有zlib.h,編譯安裝python的zlib庫卻需要這個頭文件,哪裡有?

官網啊!!!

wget http://zlib.net/zlib-1.2.8.tar.gz

還是老套路,下載、解壓、配置、編譯、安裝

再一次~

# find / -name "zlib.h"
/usr/local/include/zlib.h

有了哈哈哈

再make Python3.4,發現

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_sqlite3 _ssl _tkinter
readline
To find the necessary bits, look in setup.py in detect_modules() for the module"s name.

zlib裝好了,還有那麼多沒裝成的是什麼鬼?

(此處失誤,忘截圖了,make altinstall完提示pip安裝失敗,需要ssl/tsl)

那把_ssl也裝了吧~~咋裝?看setup.py里有,需要openssl,結果

[root@pyDev Python-3.4.4]# openssl version
OpenSSL 1.0.1e xx xxx xxx

也安了。。。。咋整?是不是似曾相識?

官網→OpenSSL

按zlib的套路再來一遍,再編譯Python,

Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-7.1.2 setuptools-18.2

pip和setuptools也自動裝上了。。。還想試試get-pip.py?當然可以~~~~

[root@pyDev Downloads]# python3.4 get-pip.py
Collecting pip
Using cached pip-8.1.1-py2.py3-none-any.whl
Collecting wheel
Using cached wheel-0.29.0-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Found existing installation: pip 7.1.2
Uninstalling pip-7.1.2:
Successfully uninstalled pip-7.1.2
Successfully installed pip-8.1.1 wheel-0.29.0
[root@pyDev Downloads]# pip3

Usage:
pip & [options]
... ...

然後再來一次=。=

find / -name "pip*"

/usr/bin/pip

/usr/bin/pip2

/usr/bin/pip2.7

/usr/local/bin/pip

/usr/local/bin/pip3

/usr/local/bin/pip3.4

好幾個名哦!安裝目錄不一樣哦!

enjoy it!

PS:openssl不怎麼好裝啊~,我這裡有幾點注意

1、配置參數

./config -fPIC --prefix=/usr/local --openssldir=/usr/local/ssl

2、結果openssl庫被裝到了/usr/local/lib64里了,make python 時找不到,需要在setup.py的初始化搜索路徑里填的東西(/usr/local/lib64 和/usr/local/include)

# lib_dirs and inc_dirs are used to search for files;
# if a file is found in one of those directories, it can
# be assumed that no additional -I,-L directives are needed.
if not cross_compiling:
lib_dirs = self.compiler.library_dirs + [
"/lib64", "/usr/lib64",
"/lib", "/usr/lib",
"/usr/local/lib64",
]
inc_dirs = self.compiler.include_dirs + ["/usr/include", "/usr/local/include"]

再make make altinstall就可以了。


參考日文自考簡單翻譯的,非常靠譜的安裝手冊,覺得好的點贊啊

1. 準備

安裝模塊

# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

Python3.4 安裝

下載代碼 configure → make → make altinstall

# cd /usr/local/src
# wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
# tar -zxvf Python-3.4.3.tgz
# cd Python-3.4.3
# ./configure --prefix=/usr/local/python
# make make altinstall

安裝Python3.4 :公用庫的安裝

# echo "/usr/local/python/lib" &>&> /etc/ld.so.conf
# ldconfig

安裝Python3.4: 4. /usr/local/bin/

# ln -s /usr/local/python/bin/python3.4 /usr/local/bin/python

安裝Python3.4 :5. 確認是否安裝正確

# /usr/local/python/bin/python3.4 -V
# python -V

安裝Python3.4 Easy_Install

# cd /usr/local/src
# wget https://pypi.python.org/…/s/setuptools/setuptools-18.0.1.zip
# unzip setuptools-18.0.1.zip
# cd setuptools-18.0.1
# /usr/local/bin/python setup.py install
# ln -s /usr/local/python/bin/easy_install /usr/local/bin/easy_install

安裝Python3.4 :Pip

# /usr/local/bin/easy_install pip
# ln -s /usr/local/python/bin/pip /usr/local/bin/pip

安裝Python3.4: Virtualenv

# pip install virtualenv
# ln -s /usr/local/python/bin/virtualenv /usr/local/bin/virtualenv

安裝Python3.4 :Virtualenvwrapper

# pip install virtualenvwrapper

安裝Python3.4 :參數設定

$ vim ~/.bashrc
if [ -f /usr/local/python/bin/virtualenvwrapper.sh ]; then
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python/bin/virtualenvwrapper.sh
fi


我今天也遇到了同樣的問題,centos6.5升級python3,python3安裝在/opt/python3目錄下面。

裝完之後使用

pip3 install

的時候提示「command not found」

修改了文件頭為#!/usr/bin/python也不起作用,最後在stackoverflow上,看到有人的安裝步驟中有「設置軟鏈接」這個步驟,試了一下:

ln -s "/opt/python3/bin/pip3" /usr/local/bin/pip

再執行

pip install pymysql

已經可以正常使用。

希望題主能夠解決問題:)


不知道題主解決了沒有。我看您的問題和評論,我理解了意思。

不知道題主安裝python3是不是鏈接到了/usr/bin/python?python3是自帶pip。但是pip使用報錯:bash : pip: command not found的時候,其實是識別不了python2還是python3要使用pip。題主可以進入你/usr/local/bin目錄查看一下是不是有pip3.4,你安裝的python3.4應該也在這裡。你在這個目錄下就可以使用pip3.4 install 來安裝軟體。

如果問題還存在,題主可以vim /usr/bin/pip看文件第一行,大概是這樣的:#!/usr/bin/python。你可以將python修該為python3.4,或者你使用python3的路徑。


分享下我自己安裝時做的筆記,從安裝python3.5.2開始

操作環境:CentOS 7

  • 編譯環境的安裝:yum install zlib-devel bzip2-devel openssl-devel ncurese-devel(要先安裝這個不然python安裝不完整,會沒有pip3)
  • cd ~/下載
  • wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
  • tar -xf Python-3.5.2.tgz
  • cd ~/下載/Python-3.5.2
  • 設置安裝目錄:./configure --prefix=/usr/bin/python-3.5.2
  • 新建安裝的文件夾:sudo mkdir /usr/bin/python-3.5.2
  • 編譯命令 :make(等待..)
  • 安裝命令:sudo make install
  • 修改默認的python版本:
  1. sudo mv /usr/bin/python /usr/bin/python.bak
  2. sudo ln -s /usr/bin/python-3.5.2/bin/python3.5 /usr/bin/python
  • 查看默認版本:python -V(如果是3.5.2,則修改完成)
  • 更改yum配置:sudo vi /usr/bin/yum(更改頭部的python為python.bak)這樣yum又可以使用python2
  • pip3使用前配置:ln -s /usr/bin/python-3.5.2/bin/pip3 /usr/bin/pip3
  • 輸入pip3 list,查看是否可以運行
  • pip3升級:pip3 install --upgrade pip

  • 我提醒一下:如果是一台被折騰了很久的電腦,有個地方需要去查看一下:

    /usr/bin下的各種鏈接

    將那些錯誤的link全部刪除後,問題都解決了。

    (我因為裝了anaconda3,所以正常來講,pip和python都會從anaconda3的文件夾下調用)


    直接發我寫的文章。一鍵腳本哦

    https://zhuanlan.zhihu.com/p/25855817?utm_source=qzone


    ./configure --prefix=/opt/soft/python --with-ensurepip=install

    沒人這麼用? 直接就把pip編譯出來了啊。


    這是我的配置方法,裝的時候仔細看README文檔會好很多,百度來的代碼不要隨便試,出現問題直接stackoverflow上找就行了:

    在CentOS 7系統中配置Python3.x


    我剛剛在centos6.5上安裝了python3.5。做了軟連接。但是/usr/local/python3.5/bin/下根本就沒有pip或者pip3啊。。

    [root@localhost opt]# python get-pip.py

    Traceback (most recent call last):

    File "get-pip.py", line 19154, in &

    main()

    File "get-pip.py", line 194, in main

    bootstrap(tmpdir=tmpdir)

    File "get-pip.py", line 82, in bootstrap

    import pip

    zipimport.ZipImportError: can"t decompress data; zlib not available

    [root@localhost opt]# yum install zlib

    Loaded plugins: fastestmirror, priorities

    Setting up Install Process

    Loading mirror speeds from cached hostfile

    * epel: http://mirrors.opencas.cn

    Package zlib-1.2.3-29.el6.x86_64 already installed and latest version

    Nothing to do

    還是不可以。


    推薦閱讀:

    python3.x 如何從str中提取bytes?
    python安裝pandas包快要崩潰了?
    Python pygame如何安裝?
    python中 from . import ×××的那個點是表示當前包嗎?
    python3.5可以安裝Twisted嗎?

    TAG:Python | Linux | Python3x | CentOS |