Psycopg2 install with pip works but cannot import module on OS X 10.9

不问归期 提交于 2019-12-05 06:32:23

OS X comes with Python 2.7.5 already; when you install Python with Homebrew, it puts a newer version in a different place without touching the built-in one. Homebrew's Python also comes with Pip, whereas OS X's doesn't. What's going on here is, you're using Homebrew's pip to install psycopg2, then running OS X's python and trying to import it.

Run /usr/local/bin/python (the full path to Homebrew's Python), and try import psycopg2 from there.

If that works, you need to put /usr/local/bin before /usr/bin in your PATH variable, so that your shell finds Homebrew's Python before the OS X one every time. If you use Bash (the default shell in OS X), you can do this by putting the following in your .bash_profile:

export PATH=/usr/local/bin:$PATH

To make sure that you're running the right Python in scripts, use the following shebang line:

#!/usr/bin/env python

env will search PATH for Python and run the script with the first one it finds, the same as typing python from your shell. Bash scripts and such should inherit the PATH variable and find the right python without changes.

You can also hardcode the path to Homebrew Python in your shebang line (#!/usr/local/bin/python), but this means your script will only work on OS X machines with a Homebrew Python installed, and is best avoided.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!