跨 python 项目共享实用程序模块

发布于 2024-08-13 03:39:55 字数 228 浏览 1 评论 0原文

在我的 python 项目中共享实用程序模块的最佳目录结构策略是什么?由于公共模块将更新新功能,我不想将它们放在 python 安装目录中。

project1/
project2/
sharedUtils/

从project1我无法使用“import ..\sharedUtils”,还有其他方法吗?我不想对“sharedUtils”位置进行硬编码

提前致谢

What would be the best directory structure strategy to share a utilities module across my python projects? As the common modules would be updated with new functions I would not want to put them in the python install directory.

project1/
project2/
sharedUtils/

From project1 I can not use "import ..\sharedUtils", is there any other way? I would rather not hardcode the "sharedUtils" location

Thanks in advance

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

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

发布评论

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

评论(3

心欲静而疯不止 2024-08-20 03:39:55

目录结构:

project1/foo.py
sharedUtils/bar.py

根据您显示的目录,您可以从 project1 目录内的 foo.py 添加到 sharedUtils 的相对路径如下所示:

import sys
sys.path.append("../sharedUtils")
import bar

这可以避免对 C:/../sharedUtils 路径进行硬编码,并且只要您不更改目录结构就可以工作。

Directory structure:

project1/foo.py
sharedUtils/bar.py

With the directories as you've shown them, from foo.py inside the project1 directory you can add the relative path to sharedUtils as follows:

import sys
sys.path.append("../sharedUtils")
import bar

This avoids hardcoding a C:/../sharedUtils path, and will work as long as you don't change the directory structure.

我偏爱纯白色 2024-08-20 03:39:55

制作一个单独的独立包?并将其放入 python 安装的 /site-packages 中?

在开发模式方面,我个人也最喜欢:使用符号链接和/或 *.pth 文件。

Make a separate standalone package? And put it in the /site-packages of your python install?

There is also my personal favorite when it comes to development mode: use of symlinks and/or *.pth files.

凤舞天涯 2024-08-20 03:39:55

假设您有 sharedUtils/utils_foosharedUtils/utils_bar
您可以编辑 PYTHONPATH 以包含 sharedUtils,然后使用在 project1project2 中导入它们。

import utils_foo
import utils_bar
etc.

在 linux 中,您可以编辑 ~/.profile像这样:

PYTHONPATH=/path/to/sharedUtils:/other/paths
export PYTHONPATH

使用 PYTHONPATH 环境变量会影响 python 在查找模块时搜索的目录。由于每个用户都可以设置自己的 PYTHONPATH,因此该解决方案非常适合个人项目。

如果您希望计算机上的所有用户都能够导入 sharedUtils 中的模块,那么
您可以通过使用 .pth 文件来实现此目的。 .pth 文件的具体放置位置可能取决于您的 python 发行版。请参阅 在 Python 中使用 .pth 文件开发

Suppose you have sharedUtils/utils_foo and sharedUtils/utils_bar.
You could edit your PYTHONPATH to include sharedUtils, then import them in project1 and project2 using

import utils_foo
import utils_bar
etc.

In linux you could do that be editing ~/.profile with something like this:

PYTHONPATH=/path/to/sharedUtils:/other/paths
export PYTHONPATH

Using the PYTHONPATH environment variable affects the directories that python searches when looking for modules. Since every user can set his own PYTHONPATH, this solution is good for personal projects.

If you want all users on the machine to be able to import modules in sharedUtils, then
you can achieve this by using a .pth file. Exactly where you put the .pth file may depend on your python distribution. See Using .pth files for Python development.

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