在 Linux 中为 Python 设置不同的 LIBDIR 路径

发布于 2024-11-25 02:09:53 字数 1036 浏览 2 评论 0原文

我想改变 Linux 下 Python 2.7 加载其模块/库的方式。我尝试从配置文件中更改它。在此之前,情况是这样的:

BINLIBDEST= $(LIBDIR)/python$(版本)
LIBDEST= $(SCRIPTDIR)/python$(版本)
INCLUDEPY= $(INCLUDEDIR)/python$(版本)
CONFINCLUDEPY= $(CONFINCLUDEDIR)/python$(版本)
LIBP= $(LIBDIR)/python$(版本)

我尝试将其更改为:

BINLIBDEST= $(LIBDIR)
LIBDEST= $(SCRIPTDIR)
包含= $(包含目录)
CONFINCLUDEPY= $(CONFINCLUDEDIR)
LIBP = $(LIBDIR)

主要是从路径名中删除 python%(VERSION),这样它就可以仅从 lib 文件夹加载其模块,而不是 lib/python27。但是,即使启动 ma​​kema​​ke install 可以处理这些更改,pythonpython27 Python 二进制文件也会不从新路径加载模块。它会返回以下输出:

找不到平台无关的库;
找不到平台相关库 
考虑将 $PYTHONHOME 设置为 [:]
“导入站点”失败;使用 -v 进行回溯

是否有一种方法可以强制 Python 二进制文件本身(如果必须)从我设置的新路径加载模块,而不是默认的“$(LIBDIR)/python” $(VERSION)”?

I would like to change the way on Python 2.7 under Linux would load its modules/libraries from. I have tried to change it from the Configure file. Before that, it was like:

BINLIBDEST=     $(LIBDIR)/python$(VERSION)
LIBDEST=        $(SCRIPTDIR)/python$(VERSION)
INCLUDEPY=      $(INCLUDEDIR)/python$(VERSION)
CONFINCLUDEPY=  $(CONFINCLUDEDIR)/python$(VERSION)
LIBP=           $(LIBDIR)/python$(VERSION)

And I attempted to change it into this:

BINLIBDEST=     $(LIBDIR)
LIBDEST=        $(SCRIPTDIR)
INCLUDEPY=      $(INCLUDEDIR)
CONFINCLUDEPY=  $(CONFINCLUDEDIR)
LIBP=           $(LIBDIR)

Mainly removing python%(VERSION) from the pathname, so that instead of lib/python27, it would simply load its modules from only lib folder. However, even if initiating make and make install works with the changes, the python or python27 Python binary file does not loads the modules from the new path. It falls back with this output:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback

Is there a way on forcing the Python binary itself (if must) to load up the modules from a new path set by me, instead of the default one as "$(LIBDIR)/python$(VERSION)"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靑春怀旧 2024-12-02 02:09:53

您必须对 python 源代码进行一些更改并重新编译,但我认为这是可以的,因为这是一个非常不标准的事情。

查看文件Modules/getpath.c。文件开头的注释中详细介绍了 python 确定 libdir 所执行的步骤。您可以在此处查看 svn 存储库。我想您会想看看如何使用这个定义:

#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
          EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"

我认为这并不像将其更改为 [...] PREFIX "/lib/:" [...],但它会是这样的。

You'll have to do a few changes to python source and recompile, but I'm going to assume that is okay, since this is a pretty non-standard thing to do.

Look at the file Modules/getpath.c. The steps python performs to determine the libdir is detailed in the comments in the beginning of the file. You can have a look at the svn repo here. I think you'll want to look at how this define is used:

#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
          EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"

I don't think it will be as easy as just changing it to [...] PREFIX "/lib/:" [...], but it will be something along those lines.

〃温暖了心ぐ 2024-12-02 02:09:53

您是否正在尝试完成 virtualenv 无法完成的事情?

看起来可以满足你的要求:

“......主要思想不是与系统范围的Python一起工作,而是
有一个独立的、更可移植的Python版本来拥有自己的
库和模块..”

Are you trying to accomplish something that virtualenv doesn't do?

It seems to meet your requirement:

"...the main idea was not to work with the system-wide Python, but to
have a separate, more of a portable version of Python to have its own
libraries and modules.."

以酷 2024-12-02 02:09:53

我要感谢 carlpett:我能够在运行时设置 python 搜索路径:在 x86_64-my_distro 中构建 Python 2.7.10 时从 lib/ 更改为 lib64/ -gnu-linux 通过修改 Modules/getpath.c 使用 gcc 5.1。

作为记录,我在配置时尝试了 --libdir (适用于共享库,但不适用于 python 模块安装路径)、修改 Makefile、修改 pyconfig.h、调整 $PYTHONPATH、$PYTHONHOME,但没有任何效果。

只是一个细节,但是 make install 没有正确放置库,所以你必须自己做一些 cp -af 和 mv 。

谢谢你卡尔佩特!!!

I'd like to thank carlpett: I was able to set python search path at runtime: changed from lib/ to lib64/ while building Python 2.7.10 in x86_64-my_distro-gnu-linux using gcc 5.1 by modifying Modules/getpath.c.

For the record, I tried --libdir at configure time (works for the shared library but not for python modules install paths), modifying Makefile, modifying pyconfig.h, tweaking $PYTHONPATH, $PYTHONHOME, nothing worked.

Just a detail, but the make install does not place correctly the libraries, so you have to do a little cp -af and mv by yourself.

THANK YOU CARLPETT!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文