在不同目录中创建多个Python模块,这些模块共享包结构的一部分 [英] Creating multiple Python modules in different directories that share a portion of the package structure

查看:138
本文介绍了在不同目录中创建多个Python模块,这些模块共享包结构的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个包含单个应用程序的Django项目。该应用程序将根据GPL发布,因此我想与该项目分开开发-使用该应用程序的个人网站。我试图在项目和应用程序中使用基于域名的包结构,这就是我遇到问题的地方。

I'm working on a Django project that contains a single application. The application will be released under the GPL so I want to develop it separately from the project - a personal site using the app. I'm attempting to use a package structure based on my domain name for both the project and the app, and that's where I'm running into problems.

这是我的文件结构(带有__init__ .py文件):

Here's my file structure (with __init__.py files where appropriate):

$HOME/django-sites/mydomain
$HOME/django-apps/mydomain/cms

我的PYTHONPATH:

And my PYTHONPATH:

$HOME/django-sites:$HOME/django-apps

如果我启动了Python解释器(从文件系统上的任何目录启动) )我可以从网站导入类,但不能从应用程序导入。如果我颠倒了PYTHONPATH中两个条目的顺序(首先是应用程序,然后是网站),则可以从应用程序导入,但不能从网站导入。

If I fire up a Python interpreter (from any directory on the filesystem) I can import classes from the site, but not the application. If I reverse the order of the two entries in the PYTHONPATH (apps first, then sites) I can import from the app but not the site.

这似乎是Python唯一尝试从中导入的PYTHONPATH中的第一个条目,其中包含包名称的第一部分。那是对的吗?这是预期的行为吗?如果是这样,我只能将模块粘贴在域/ app1,domain / app2之类的包结构中(如果它们位于相同的目录结构中),而与PYTHONPATH无关。

It looks like Python's only attempting to import from the first entry in the PYTHONPATH that contains the first portion of the package name. Is that correct? Is this expected behavior? If so, I can only stick modules in package structures like domain/app1, domain/app2 if they live in the same directory structure - regardless of the PYTHONPATH.

这不是show-stopper,因为我可以重命名该站点,但是它与我期望的有很大不同。 Python教程提到了__path__,但是我不知道如何使用它:

It's not show-stopper because I can rename the site, but it's much different than I was expecting. The Python tutorial mentions __path__, but I have no idea how to use it:


包还支持一种特殊的属性__path__。在执行该文件中的代码之前,该文件初始化为包含目录的名称的列表,该目录包含软件包的__init__.py。这个变量可以修改;这样做会影响以后对包中包含的模块和子包的搜索。

Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

虽然此功能不是经常需要的,但可用于扩展包中找到的模块集。

While this feature is not often needed, it can be used to extend the set of modules found in a package.

还有其他人遇到吗?我可以使用__path__来实现此功能吗?

Has anyone else come across this? Is there something I can do with __path__ to make this function as expected?

推荐答案

您的分析似乎正确。 python路径用于定位要导入的模块; python导入它找到的第一个。除了将模块命名不同或将它们放在搜索路径中的相同位置之外,我不确定您能做什么来解决此问题。

Your analysis seems about right. The python path is used to locate modules to import; python imports the first one it finds. I'm not sure what you could do to work around this other than name your modules different things or put them in the same location in the search path.

这篇关于在不同目录中创建多个Python模块,这些模块共享包结构的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆