如何在Linux上使我的Python模块在系统上可用?

时间:2022-06-19 16:57:01

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?

有没有人知道我需要将模块复制到系统范围内?

5 个解决方案

#1


8  

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.py which 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权限以在系统范围内安装模块。有好的和简单的例子,加上它是我能想象的最干净的方式。

#2


3  

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 +文件是可读的,那么您应该被允许导入它。

#3


1  

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

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

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

#4


1  

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。

#5


0  

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提示符。

#1


8  

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.py which 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权限以在系统范围内安装模块。有好的和简单的例子,加上它是我能想象的最干净的方式。

#2


3  

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 +文件是可读的,那么您应该被允许导入它。

#3


1  

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

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

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

#4


1  

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。

#5


0  

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提示符。