如何使我的 Python 模块在 Linux 系统范围内可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17236675/
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-06 23:16:28  来源:igfitidea点击:

How to make my Python module available system wide on Linux?

pythonlinuxmoduleinstallation

提问by kramer65

I made myself a little module which I happen to use quite a lot. Whenever I need it I simply copy it to the folder in which I want to use it. Since I am lazy I wanted to install it so that I can call it from anywhere, even the interactive prompt. So I read a bit about installing here, and concluded I needed to copy the file over to /usr/local/lib/python2.7/site-packages. That however, doesn't seem to do anything.

我给自己做了一个小模块,我碰巧使用了很多。每当我需要它时,我只需将它复制到我想使用它的文件夹中。由于我很懒,我想安装它以便我可以从任何地方调用它,甚至是交互式提示。所以我读了一些关于在此处安装的内容,并得出结论我需要将文件复制到 /usr/local/lib/python2.7/site-packages。然而,这似乎没有任何作用。

Does anybody know where I need to copy my module for it to work system wide?

有谁知道我需要在哪里复制我的模块才能在系统范围内工作?

采纳答案by Johannes P

There are methods to install Python modules system-wide. You may want to take a look at distutils. A good tutorial for distutils2 (the current version) can be found here.

有一些方法可以在系统范围内安装 Python 模块。您可能想看看distutils。一个很好的 distutils2(当前版本)教程可以在这里找到。

You basically have to write a file setup.pywhich tells distutils what to do. Then you can simply

您基本上必须编写一个文件setup.py来告诉 distutils 要做什么。然后你可以简单地

python setup.py install

with root permissions to install your module systemwide. There are good and easy examples, plus it's the cleanest way I can imagine.

具有 root 权限以在系统范围内安装您的模块。有很好和简单的例子,而且这是我能想象的最干净的方式。

回答by falsetru

If you're using Ubuntu, copy files to /usr/local/lib/python2.7/dist-packages. Following command will show you where to copy.

如果您使用的是 Ubuntu,请将文件复制到/usr/local/lib/python2.7/dist-packages. 以下命令将显示复制的位置。

python -c "from distutils.sysconfig import *; print(get_python_lib())"

If you are the only one use the module, copy files to ~/.local/lib/python2.7/site-packages.

如果您是唯一一个使用该模块的人,请将文件复制到~/.local/lib/python2.7/site-packages.

回答by michaelmeyer

In one of the directories listed when you type sys.pathin your Python prompt. You can also add the directory which contains your file by modifiying the PYTHONPATHenvironment variable:

在您键入sys.pathPython 提示符时列出的目录之一中。您还可以通过修改PYTHONPATH环境变量来添加包含文件的目录:

# ~/.bashrc file
export PYTHONPATH+=:/some/dir

回答by cSn

The answer is: it's all about permissions.

答案是:一切都与权限有关

It's not enough to place the file in the correct location, like, such instance: /usr/local/lib/python2.7/dist-packages, you also need to ensure that the file can be read by the process you're running, in this case, python.

将文件放在正确的位置是不够的,例如:/usr/local/lib/python2.7/dist-packages,您还需要确保您正在运行的进程可以读取该文件,在这种情况下,python。

Be sure that "other" users have read access to the file. Open the bash console and execute this:

确保“其他”用户对该文件具有读取权限。打开 bash 控制台并执行以下命令:

sudo chmod o+r "yourmodule.py"
[Introduce the password]

After this go again to python and try the import:

在此之后再次转到 python 并尝试导入:

import "yourmodule"

As long as the path where the .py file is located is present in PYTHONPATH + the file is readable then you should be allowed to import it.

只要 .py 文件所在的路径存在于 PYTHONPATH 中 + 该文件是可读的,那么您就应该被允许导入它。

回答by Brian

Couple of things.

几件事。

First the module must, (I believe), be in a directory that matches the module name.

首先,模块必须(我相信)位于与模块名称匹配的目录中。

Put that module directory under one of the directories in the PYTHONPATH (I use /usr/lib/pymodules/pythonV.x/). You can find a suitable directory in the path using

将该模块目录放在 PYTHONPATH 中的目录之一下(我使用 /usr/lib/pymodules/pythonV.x/)。您可以使用以下命令在路径中找到合适的目录

import sys
print(sys.path)

from the python prompt.

从 python 提示。