在Python中,如何有效地管理脚本文件之间的引用?
我有相当多的 Python 脚本,其中包含其他 Python 脚本使用和引用的可重用代码。 然而,这些脚本往往分散在不同的目录中,我发现必须在顶级脚本上包含(通常是多个)对 sys.path.append 的调用有点乏味。 我只想提供“导入”语句,而无需在同一脚本中提供其他文件引用。
目前,我有这样的:
import sys
sys.path.append('..//shared1//reusable_foo')
import Foo
sys.path.append('..//shared2//reusable_bar')
import Bar
我的偏好如下:
import Foo
import Bar
我的背景主要是.NET平台,所以我习惯使用*.csproj、*.vbproj、*.sln等元文件来管理和包含源文件外部的实际文件路径引用。 这允许我只提供“using”指令(相当于 Python 的导入),而无需公开所有引用,并允许在多个脚本中重用路径引用本身。
Python 是否对此有同等支持?如果没有,有哪些技术和方法?
I have a fair number of Python scripts that contain reusable code that are used and referenced by other Python scripts. However, these scripts tend to be scattered across different directories and I find it to be somewhat tedious to have to include (most often multiple) calls to sys.path.append on my top-level scripts. I just want to provide the 'import' statements without the additional file references in the same script.
Currently, I have this:
import sys
sys.path.append('..//shared1//reusable_foo')
import Foo
sys.path.append('..//shared2//reusable_bar')
import Bar
My preference would be the following:
import Foo
import Bar
My background is primarily in the .NET platform so I am accustomed to having meta files such as *.csproj, *.vbproj, *.sln, etc. to manage and contain the actual file path references outside of the source files. This allows me to just provide 'using' directives (equivalent to Python's import) without exposing all of the references and allowing for reuse of the path references themselves across multiple scripts.
Does Python have equivalent support for this and, if not, what are some techniques and approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的解决方案是打包一个可以导入模块的实用程序:
my_util 在站点包中
My solution was to package up one utility that would import the module:
my_util is in site packages
如果您的可重用文件已打包(即,它们包含
__init__.py
文件)并且该包的路径是 PYTHONPATH 或 sys.path 的一部分,那么您应该能够执行这个问题提供了更多详细信息。
(注意:正如吉姆所说,您还可以将可重用代码放入您的
site-packages
目录中。)If your reusable files are packaged (that is, they include an
__init__.py
file) and the path to that package is part of your PYTHONPATH or sys.path then you should be able to do justThis question provides a few more details.
(Note: As Jim said, you could also drop your reusable code into your
site-packages
directory.)您可以将可重用的东西放在
site-packages
中。 这是完全透明的,因为默认情况下它位于 sys.path 中。您可以将
someName.pth
文件放入site-packages
中。 这些文件包含实际可重用内容所在的目录。 这也是完全透明的。 并且不涉及在site-packages
中安装更改的额外步骤。您可以将可重用内容的目录放在
PYTHONPATH
上。 这有点不太透明,因为您必须确保它已设置。 不是火箭科学,但也不是完全透明的。You can put the reusable stuff in
site-packages
. That's completely transparent, since it's insys.path
by default.You can put
someName.pth
files insite-packages
. These files have the directory in which your actual reusable stuff lives. This is also completely transparent. And doesn't involve the extra step of installing a change insite-packages
.You can put the directory of the reusable stuff on
PYTHONPATH
. That's a little less transparent, because you have to make sure it's set. Not rocket science, but not completely transparent.在一个项目中,我想确保用户可以将 python 脚本(基本上可以用作插件)放在任何地方。 我的解决方案是将以下内容放入该项目的配置文件中:
这样,这将在程序启动时将 /home/jason 和 /usr/share/some_directory 添加到 python 路径。
然后,只需用冒号分割字符串并将这些目录添加到 sys.path 的末尾即可。 您可能需要考虑将一个模块放在 site-packages 目录中,该目录包含一个读取该配置文件并将这些目录添加到 sys.path 的函数(不幸的是,我现在没有时间编写示例) 。
正如其他人提到的,最好将尽可能多的内容放入站点包中并使用 .pth 文件。 但是,如果您有一个脚本需要导入一堆不在站点包中且您不想从其他脚本导入的内容,那么这可能是一个好主意。
(也可能有一种方法可以使用 .pth 文件来执行此操作,但我喜欢能够在放置其余配置信息的同一位置操作 python 路径)
In one project, I wanted to make sure that the user could put python scripts (that could basically be used as plugins) anywhere. My solution was to put the following in the config file for that project:
That way, this would add /home/jason and /usr/share/some_directory to the python path at program launch.
Then, it's just a simple matter of splitting the string by the colons and adding those directories to the end of the sys.path. You may want to consider putting a module in the site-packages directory that contains a function to read in that config file and add those directories to the sys.path (unfortunately, I don't have time at the moment to write an example).
As others have mentioned, it's a good idea to put as much in site-packages as possible and also using .pth files. But this can be a good idea if you have a script that needs to import a bunch of stuff that's not in site-packages that you wouldn't want to import from other scripts.
(there may also be a way to do this using .pth files, but I like being able to manipulate the python path in the same place as I put the rest of my configuration info)
最简单的方法是设置(或添加到)PYTHONPATH,并将模块和包放入(或符号链接)到 PYTHONPATH 中包含的路径中。
The simplest way is to set (or add to) PYTHONPATH, and put (or symlink) your modules and packages into a path contained in PYTHONPATH.
简单的答案是将可重用代码放在 site-packages 目录中,该目录位于 sys.path 中。
您还可以通过在路径中的某处添加 .pth 文件来扩展搜索路径。
请参阅 https://docs.python.org/2/ install/#modifying-python-s-search-path 了解更多详细信息
哦,python 2.6/3.0 添加了对 PEP370 的支持,每用户站点包目录
The simple answer is to put your reusable code in your site-packages directory, which is in your sys.path.
You can also extend the search path by adding .pth files somewhere in your path.
See https://docs.python.org/2/install/#modifying-python-s-search-path for more details
Oh, and python 2.6/3.0 adds support for PEP370, Per-user site-packages Directory