关于pip:pip3安装在虚拟环境中,python3.6由于ssl模块不可用而失败

pip3 installs inside virtual environment with python3.6 failing due to ssl module not available

(py36venv) vagrant@pvagrant-dev-vm:/vagrant/venvs$ pip3 install pep8

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

Collecting pep8 Could not fetch URL
https://pypi.python.org/simple/pep8/: There was a problem confirming
the ssl certificate: Can't connect to HTTPS URL because the SSL module
is not available. - skipping

Could not find a version that satisfies the requirement pep8 (from
versions: ) No matching distribution found for pep8

背景信息 - 试图转向python 3.6。

使用以下命令安装python3.6:

wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz

tar -xvf Python-3.6.0.tgz

cd Python-3.6.0
./configure --enable-optimizations
make -j8 sudo
make altinstall python3.6

通过以下方式创建virtualenv:

python3.6 -m venv py36venv

source py36venv/bin/activate

试图安装pep8

(py36venv) pip3 install pep8

pip is configured with locations that require TLS/SSL, however the ssl
module in Python is not available.
Collecting pep8

Could not fetch URL https://pypi.python.org/simple/pep8/: There was a problem
confirming the ssl certificate: Can't connect to HTTPS URL because the

SSL module is not available. - skipping Could not find a version
that satisfies the requirement pep8 (from versions: ) No matching
distribution found for pep8


我按照以下步骤在ubuntu 14.04中安装python3.6并且virtualenv pip installs工作正常。

Python 3.6安装:

1
2
3
4
5
6
7
8
sudo apt-get install python3-dev libffi-dev libssl-dev
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz  
tar xvf Python-3.6.0.tgz
cd Python-3.6.0
./configure --enable-optimizations  
make -j8  
sudo make altinstall
python3.6

如果看到以下错误 -

zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [altinstall] Error 1

尝试:

1
sudo apt-get install zlib1g-dev

验证:

在python3.6中创建virtualenv:

1
2
3
python3.6 -m venv testenv
source testenv/bin/activate
pip install pep8

使用点子:

1
2
3
4
5
6
7
8
9
10
11
12
(testenv) vagrant@pvagrant-dev-vm:~$ pip install pep8
*Collecting pep8
  Downloading pep8-1.7.0-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 4.1MB/s
Installing collected packages: pep8
Successfully installed pep8-1.7.0*

(testenv) vagrant@pvagrant-dev-vm:~$ pip list

pep8 (1.7.0)
pip (9.0.1)
setuptools (28.8.0)


当我尝试使用python3.6.0创建虚拟环境时,我偶然发现了同样的问题。这是我的Mac OS X 10.12.2解决方案(Py_minion评论非常接近):

建立

我通过以下步骤创建了环境:
下载python3.6.0
赛跑

1
2
3
4
./configure --prefix=<some_path>`
make
make install
mkvirtualenv --python=<some_path/bin/python3.6> foo

所以基本类似于:https://stackoverflow.com/a/11301911/1286093

如果您遇到与我相同的问题,则表示在运行make时类似的行

1
The necessary bits to build these optional modules were not found: _ssl

安装openssl

1
2
brew install openssl
brew unlink openssl && brew link openssl --force

更改模块/设置或模块/ Setup.dist

您可以在下载的Python版本的目录中找到这些文件。

评论并在必要时更改第209-211行(我必须将SSL变量更改为我的openssl位置)。

1
2
3
4
SSL=/usr/local/opt/openssl <---- THIS DEPENDS ON YOUR INSTALLATION
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

鉴于这是openssl的位置

设置环境变量

1
2
export CFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"

再次制作和安装

运行

1
2
3
4
./configure --prefix=<some_path>`
make
make install
mkvirtualenv --python=<some_path/bin/python3.6> foo

再次为我做了伎俩


在CentOS 7下从源代码构建Python 3.6.1时遇到了同样的错误。
对于CentOS7,我必须先:

1
sudo yum install openssl-dev

然后:

1
2
./configure --enable-optimizations
make altinstall

现在pip3.6有效:-)


运行make在shell输出中向我报告:

1
2
3
4
The necessary bits to build these optional modules were not found:
_bz2                  _dbm                  _gdbm              
_sqlite3              _ssl                  _tkinter          
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

什么解决了我的问题(Linux Mint 18.1,openssl已安装)正在编辑Python-3.6.0文件夹中的setup.py,添加了openssl安装将ssl.h文件放入我的系统的路径( /usr/include/openssl/ssl.h)。这里我添加了行'/ usr / include'的部分:

1
2
3
4
5
6
7
8
9
    # Detect SSL support for the socket module (via _ssl)
    search_for_ssl_incs_in = [
                          '/usr/local/ssl/include/',
                          '/usr/contrib/ssl/include/',
                          '/usr/include/'
                         ]
    ssl_incs = find_file('openssl/ssl.h', inc_dirs,
                         search_for_ssl_incs_in
                         )


我在Ubuntu-16.04.1上解决了这个问题。
首先,您需要安装必要的库。要安装打开的终端(Ctrl + Alt + T),然后键入;

1
2
 sudo apt-get install build-essential checkinstall
 sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

之后转到python文件所在的目录,然后重新配置并重新安装python3.6。

1
2
3
4
 cd /opt/Python3.6/
./configure
 make
sudo make install

注意
如果你通过ppa安装了Python3.6,那么重新安装它;

1
sudo apt-get install python3.6

现在你应该可以使用pip3.6了


A complete script can be found HERE

安装先决条件

对于RHEL / CentOS

sudo yum -y install gcc gcc-c++ zlib zlib-devel libffi-devel openssl-devel wget

对于Ubuntu / Debian

sudo apt-get -y install build-essential python-dev python-setuptools python-pip
python-smbus libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev
tk-dev libssl-dev openssl libffi-dev wget

下载Python

Modify for the version of python you want

Python Versions

cd /var/tmp
sudo wget https://www.python.org/ftp/python/3.x.x/Python-x.x.x.tgz
sudo tar xf Python-3.*.tgz
cd Python-3*

配置/制作/安装

sudo ./configure --enable-optimizations --enable-shared --prefix=/usr/local
sudo make && make altinstall

清理共享库并添加到路径

剥离调试符号的共享库可以加快执行并行脚本时的执行速度。

<5233>

参考

  • 要旨
  • 堆栈溢出
  • 堆栈溢出
  • 丹尼尔埃里克森
  • Unix StackExchange
  • TLDP