Python 找不到 Psycopg2 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16407995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:32:28  来源:igfitidea点击:

Psycopg2 image not found

pythonmacospostgresqlpsycopg2

提问by Craig Cannon

Trying to setup postgres with the postgres mac app and hit this error, which I haven't been able to solve. Any thoughts?

尝试使用 postgres mac 应用程序设置 postgres 并遇到此错误,我无法解决。有什么想法吗?

    ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib
  Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
  Reason: image not found

回答by Ari

Did you install psycopg2? You need it in order to integrate python with postgresql.

你安装了 psycopg2 吗?您需要它才能将 python 与 postgresql 集成。

回答by Mr. B

In your bash environment before you load it, try this:

在加载之前在 bash 环境中,尝试以下操作:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/x.y/lib

..replacing the 'x.y' with the version on your system.

..用系统上的版本替换“xy”。

..be aware that setting this in your bash profile can interfere with other programs, as KindOfGuy noted.

..请注意,正如 KindOfGuy 指出的那样,在您的 bash 配置文件中设置它会干扰其他程序。

..of course, if you're not running it from a bash prompt, you'll have to set up your environment in whatever way pyenv lets you. ..you could even edit pyenv itself and place that at the top.

..当然,如果您不是从 bash 提示符运行它,则必须以 pyenv 允许的任何方式设置您的环境。..你甚至可以编辑 pyenv 本身并将其放在顶部。

Another alternative is to put this in a python script which runs before you attempt to import psycopg2:

另一种选择是将它放在一个 python 脚本中,该脚本在您尝试导入 psycopg2 之前运行:

import os
os.environ['DYLD_LIBRARY_PATH'] = '/Library/PostgreSQL/x.y/lib'

..again, replacing 'x.y' with the version on your system in /Library/PostgreSQL.

..再次,将“xy”替换为您系统上 /Library/PostgreSQL 中的版本。

回答by philippinedev

$ sudo ln -s /Library/PostgreSQL/9.2/lib/libssl.1.0.0.dylib /usr/lib
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libcrypto.1.0.0.dylib /usr/lib

I encountered this error while working on Django. I have it working on virtualenv with Django==1.3 but not on Django==1.5 where I have to issue the commands above.

我在 Django 上工作时遇到了这个错误。我让它在 Django==1.3 的 virtualenv 上工作,但不是在 Django==1.5 上我必须发出上面的命令。

In OS X El Capitan you can't do these links without disabling system protection but it works well if you link to /usr/local/lib

在 OS X El Capitan 中,您不能在不禁用系统保护的情况下执行这些链接,但如果您链接到 /usr/local/lib 则效果很好

回答by stephenalexbrowne

I found a solution that worked for me when dealing with a similar issue on rails. Add the following to your .bash_profile, .bash_rc, or equivalent:

在处理 Rails 上的类似问题时,我找到了一个对我有用的解决方案。将以下内容添加到您的 .bash_profile、.bash_rc 或等效文件中:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib:$DYLD_LIBRARY_PATH

(Assuming you installed Postgres.app in the default location). Then restart your terminal session and try again.

(假设您在默认位置安装了 Postgres.app)。然后重新启动终端会话并重试。

Exporting to DYLD_LIBRARY_PATH directly can cause serious problems with other apps that depend on it, but using the fallback path avoids these problems.

直接导出到 DYLD_LIBRARY_PATH 可能会导致其他依赖它的应用程序出现严重问题,但使用回退路径可以避免这些问题。

See also: https://github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546

另见:https: //github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546

EDIT: It seems that setting DYLD_FALLBACK_LIBRARY_PATH causes an error when you try to run psql. To fix this, you can add the following two lines to your .bash_profile:

编辑:当您尝试运行 psql 时,似乎设置 DYLD_FALLBACK_LIBRARY_PATH 会导致错误。要解决此问题,您可以将以下两行添加到 .bash_profile 中:

alias psql="(. ~/.bash_profile; unset DYLD_FALLBACK_LIBRARY_PATH; psql)";

This is assuming that you're using bash and that your .bash_profile is located in your home directory. If that's not the case (or if you're using a .bashrc or other environment setup instead of .bash_profile) change the ~/.bash_profilepart of the command to the path to your environment setup script.

这是假设您正在使用 bash 并且您的 .bash_profile 位于您的主目录中。如果不是这种情况(或者如果您使用的是 .bashrc 或其他环境设置而不是 .bash_profile)~/.bash_profile,请将命令的一部分更改为您的环境设置脚本的路径。

The aliased command basically starts a subshell which does not effect your current bash environment. So when it unsets the DYLD_FALLBACK_LIBRARY_PATH variable, it's only temporary. After you exit psql the environment variable will be set again.

aliased 命令基本上会启动一个不会影响您当前 bash 环境的子 shell。因此,当它取消设置 DYLD_FALLBACK_LIBRARY_PATH 变量时,它只是暂时的。退出 psql 后,将再次设置环境变量。

回答by jdeng

This problem cost me the whole morning to solve. I found the discussion on http://initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os-x/really helpful. Thanks to Jurie's answer, the solution to my problem (in Mac) is as below:

这个问题花了我整个上午才解决。我发现http://initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os-x/上的讨论真的很有帮助。感谢 Jurie 的回答,我的问题(在 Mac 中)的解决方案如下:

  1. Install openssl 1.0.0 using brew:

     brew install openssl
    
  2. using the following command:

    export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/**1.0.1x**/lib
    

    replace 1.0.1xpart with your current version. For me it is 1.0.1h.

  1. 使用 brew 安装 openssl 1.0.0:

     brew install openssl
    
  2. 使用以下命令:

    export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/**1.0.1x**/lib
    

    用您当前的版本替换1.0.1x部分。对我来说是 1.0.1 小时。

Hope this helps!

希望这可以帮助!

EDIT: After one day, I found that the second command has to be entered every time when needing to connect to database, so not a permanent solution to this problem.

编辑:一天后,我发现每次需要连接数据库时都必须输入第二个命令,所以不是这个问题的永久解决方案。

回答by Mit Mehta

I found this solution that worked for me

我找到了这个对我有用的解决方案

sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libssl.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libssl.1.0.0.dylib /usr/lib/libssl.dylib 

sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libcrypto.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libcrypto.1.0.0.dylib /usr/lib/libcrypto.dylib 

Replace this part '/Applications/Postgres.app/Contents/Versions/9.3/' depending upon the location where psql is installed on your machine. Command to find where psql is installed :which psql

根据您机器上安装 psql 的位置,替换这部分“/Applications/Postgres.app/Contents/Versions/9.3/”。查找 psql 安装位置的命令:which psql

UPDATE FROM COMMENTS: On OSX 10.11 (El Capitan), you can no longer copy files to /usr/lib. Use /usr/local/lib

评论更新:在 OSX 10.11 (El Capitan) 上,您不能再将文件复制到 /usr/lib。使用 /usr/local/lib

回答by agentargo

I was missing the postgresql client package, so I installed them with brew and that fixed this issue for me.

我缺少 postgresql 客户端包,所以我用 brew 安装了它们,并为我解决了这个问题。

brew update
brew doctor
brew install postgresql

回答by duozmo

Big picture, the problem is that a required library couldn't be found. You can alter where psycopg2looks for libsslusing Apple's open source compiler tools, otooland install_name_tool. These ship with OS X, and manual pages are available with man <command>.

大图,问题是找不到所需的库。您可以使用 Apple 的开源编译器工具更改psycopg2查找位置,并且. 这些随 OS X 一起提供,手册页随.libsslotoolinstall_name_toolman <command>

Change into the psycopg2module directory mentioned in the error message. Once there:

切换到psycopg2错误消息中提到的模块目录。一旦到达:

$ otool -L _psycopg.so
    ...
    @executable_path/../lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
    ...

This lists the libraries _psycopg2.sowill look for. You can change where it'll look with install_name_tool:

这列出了_psycopg2.so将要查找的库。您可以使用以下命令更改它的外观install_name_tool

$ install_name_tool -change @executable_path/../lib/libssl.1.0.0.dylib /usr/local/opt/openssl/lib/libssl.1.0.0.dylib _psycopg.so

You'll need to adjust for where you have libssl.1.0.0.dylib, of course. The example I gave is the default Homebrew path, but you might have it from Anaconda and/or the PostgreSQL app bundle. (brew install opensslif you don't have it yet.) You'll also likely need to repeat for libcrypto.

当然,您需要根据您拥有的位置进行调整libssl.1.0.0.dylib。我给出的示例是默认的 Homebrew 路径,但您可能从 Anaconda 和/或 PostgreSQL 应用程序包中获得它。(brew install openssl如果您还没有它。)您可能还需要为 libcrypto 重复。

Executing this change may fail depending on how _psycopg2.sowas built. If that happens, you could probably build the module yourself with custom library paths, but I won't get into that.

执行此更改可能会失败,具体取决于_psycopg2.so构建方式。如果发生这种情况,您可能可以使用自定义库路径自己构建模块,但我不会涉及。

This approach has the advantage of being narrower, and thus less risky, than the approach (given in other answers here) of linking libssl 1.0.0 into dyld's search paths (either through ln -sor setting a DYLD_*environment variable). (See warnings against these approaches in a pairof discussionsand some code. Learn more about dyldthrough man dyld.) It has the disadvantage of needing to be repeated for each copy of psycopg2. Choose your own adventure.

与将 libssl 1.0.0 链接到dyld的搜索路径(通过ln -s或设置DYLD_*环境变量)的方法(在此处的其他答案中给出)相比,这种方法的优点是更窄,因此风险更小。(见警告反对这些在接近一双讨论一些代码。了解更多关于dyld通过man dyld)。它需要被重复的每个副本的缺点psycopg2。选择你自己的冒险。

Disclaimer: Most of the content in this answer is from knowledge I cobbled together in one day. I am no expert.

免责声明:此答案中的大部分内容来自我在一天内拼凑出来的知识。我不是专家。

回答by therealmarv

I had the same error because of upgrading Postgres.app (from 9.3 to 9.4).

由于升级 Postgres.app(从 9.3 到 9.4),我遇到了同样的错误。

The solution was to delete the pip cache wheels because they were pointing to Postgres.app version 9.3.

解决方案是删除 pip 缓存轮,因为它们指向 Postgres.app 版本 9.3。

I've found the according psycopg wheel cache files this way

我通过这种方式找到了相应的 psycopg 轮缓存文件

grep -r psycopg ~/.pip/cache

and deleted the directory I've found with the last command.

并删除了我用最后一个命令找到的目录。

回答by aeikenberry

This happened to me after upgrading Postgresql, and after installing psycopg2 in my virtualenv. Reinstalling (re-building) worked for me.

在升级 Postgresql 以及在我的 virtualenv 中安装 psycopg2 之后,这发生在我身上。重新安装(重新构建)对我有用。

pip uninstall psycopg2
pip install psycopg2