如何在 mac 上使用 virtualenv 在 Python3.6 上安装 psycopg2?

How to install psycopg2 on mac with virtualenv on Python3.6?

我是 运行 OSX 10.12.2 (16C67) 和 3.6.0 inside virtualenv。

我已经测试了以下内容。

➜  ~ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

➜  ~ pg_config
BINDIR = /usr/local/Cellar/postgresql/9.6.1/bin
DOCDIR = /usr/local/Cellar/postgresql/9.6.1/share/doc/postgresql
HTMLDIR = /usr/local/Cellar/postgresql/9.6.1/share/doc/postgresql
INCLUDEDIR = /usr/local/Cellar/postgresql/9.6.1/include
PKGINCLUDEDIR = /usr/local/Cellar/postgresql/9.6.1/include
INCLUDEDIR-SERVER = /usr/local/Cellar/postgresql/9.6.1/include/server
LIBDIR = /usr/local/lib
PKGLIBDIR = /usr/local/lib/postgresql
LOCALEDIR = /usr/local/Cellar/postgresql/9.6.1/share/locale
MANDIR = /usr/local/Cellar/postgresql/9.6.1/share/man
SHAREDIR = /usr/local/share/postgresql
SYSCONFDIR = /usr/local/etc/postgresql
PGXS = /usr/local/lib/postgresql/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--disable-debug' '--prefix=/usr/local/Cellar/postgresql/9.6.1' '--datadir=/usr/local/share/postgresql' '--libdir=/usr/local/lib' '--sysconfdir=/usr/local/etc' '--docdir=/usr/local/Cellar/postgresql/9.6.1/share/doc/postgresql' '--enable-thread-safety' '--with-bonjour' '--with-gssapi' '--with-ldap' '--with-openssl' '--with-pam' '--with-libxml' '--with-libxslt' '--with-perl' '--with-tcl' '--with-tclconfig=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/lib' '--with-uuid=e2fs' 'CC=clang' 'LDFLAGS=-L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' 'CPPFLAGS=-I/usr/local/opt/openssl/include -I/usr/local/opt/readline/include'
CC = clang
CPPFLAGS = -DFRONTEND -I/usr/local/opt/openssl/include -I/usr/local/opt/readline/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -O2
CFLAGS_SL =
LDFLAGS = -L../../src/common -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib -Wl,-dead_strip_dylibs
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lm
VERSION = PostgreSQL 9.6.1

➜  ~ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

已检查 XCode 工具是否已安装。但是我在尝试安装 psycopg2 时不断出错。任何想法下一步该做什么。

** 编辑

我只是尝试使用相同的 Python 版本在 virtualenv 之外安装它,并且安装没有任何问题。

➜  ~ pip3 install psycopg2
Collecting psycopg2
  Using cached psycopg2-2.6.2.tar.gz
Building wheels for collected packages: psycopg2
  Running setup.py bdist_wheel for psycopg2 ... done
  Stored in directory: /Users/Kimmo/Library/Caches/pip/wheels/49/47/2a/5c3f874990ce267228c2dfe7a0589f3b0651aa590e329ad382
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.

6.2

似乎我的配置 pip install -r requirements.txt 在使用 python3 时失败了。

我通过 运行 pip uninstall -r requirements.txt 然后 运行 pip3 install -r requirements.txt

解决了这个问题

Python 2 和 Python 3 都有一个与之关联的 pip 实用程序。因此,如果将两者安装在同一目录中,则第二个安装 运行 存在覆盖第一个 pip 的风险。

运行 python2 -m pippython3 -m pip 总是最安全的,如果你想确定你是哪个版本的话 运行ning。

一旦您正确激活了虚拟环境,its pip 应该是默认 运行s 的那个,确保安装在里面进行虚拟环境。如果 virtualenv 不包含 pip,你会发现将软件包安装到你的 virtualenv 中会更加困难。