重复使用带有不同脚本的模块(代码组织) [英] Reuse module with different scripts (code organization)

查看:123
本文介绍了重复使用带有不同脚本的模块(代码组织)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个utils软件包,它为我的(crontab)Python脚本提供功能。



这些脚本在同一路径下的不同目录中组织:



例如:

  scripts / client1 / process-client1.py 
.. /client2/process-client2.py
.. /client3/process-client3.py
.. /utils/foo.py

所有这些文件都生活在SVN存储库中的类似设置中(除了每个文件都在实际代码之前具有trunk /和tags /,因此我在需要时将它们导出到生产环境中)



utils软件包中的模块应可用于所有脚本(utils软件包中确实包含 __ init __。py



我在process-client1.py中尝试了相对导入:



import ..utils



但是,很明显,我得到了:
ValueError:尝试以非包装方式进行相对导入



还有其他方法吗?我不想在每个客户端目录中使用utils /,因为它实际上是所有脚本都需要的通用软件包,如果我进行了更改,则应该在使用该脚本的所有脚本中反映出来。



将客户端脚本转换为程序包听起来也不对,因为实际上不是。另外,该如何工作,我尝试将空的 __ init __。py 放在/ scripts /和/ scripts / client * /目录中,但这还是无济于事。



可以为每个客户端做符号链接到../utils/,但这是需要跟踪的其他信息,客户端列表是改变并且听起来不正确(但是我希望我还是会这样做)。



我看着作为学习Python的Java程序员,我应该注意什么?,但这在这里并没有真正描述。在 python源代码组织和__init.py__ 中-我似乎有类似的设置,但是我不能从同级目录中导入utils或..utils?

解决方案

Q& D解决方案将是在导入 utils 之前,将目录的路径添加到 sys.path 中:

 #process-client-XXX.py 
import os,sys
root = os.path.dirname( os.path.dirname(os.path.abspath(__ file)))
如果root不在sys.path中:
sys.path.insert(0,root)

from utils import foo

#您的代码在这里

这确实很丑,但是

干净的解决方案是正确打包 utils 因此您可以使用pip或类似的东西来部署它。 ch从长远来看更容易维护。


I have a utils package which provides functions for my (crontab) Python scripts.

The scripts are organized in different directories on the same path:

Eg.:

scripts/client1/process-client1.py
..     /client2/process-client2.py
..     /client3/process-client3.py
..     /utils/foo.py

All of these files live in similar setup in SVN repository (except each has trunk/ and tags/ before actual code, so I export them to production whenever needed)

The modules in utils package should be available to all scripts (the utils package does contain __init__.py)

I tried relative import, in process-client1.py:

import ..utils

However, obviously I got: ValueError: Attempted relative import in non-package

Is there any other way? I wouldn't like to utils/ in each client dir, as it is really a common package that is needed by all scripts, and if I make a change in it, it should be reflected in all scripts using it.

It wouldn't sound right also to convert client scripts to packages, as they are really not. Plus, how would that work, I tried putting empty __init__.py in /scripts/ and /scripts/client*/ dirs, but that didn't help anyway.

I could do symlink for each client to ../utils/, however, this is additional info that needs to be tracked, the clients list is changing and it doesn't sound right (but I expect I might end up doing it anyway).

I looked at As a Java programmer learning Python, what should I look out for? , but this is not really described there. At python source code organization and __init.py__ -- I seem to have similar setup, but I can't import either utils or ..utils from sibling dir?

解决方案

The Q&D solution would be to add the path to the "scripts" directory to your sys.path before you import utils:

# process-client-XXX.py
import os, sys
root = os.path.dirname(os.path.dirname(os.path.abspath(__file)))
if root not in sys.path:
    sys.path.insert(0, root)

from utils import foo

# your code here

This is really ugly but well it should work as long as your directory layout doesn't change.

The clean solution would be to properly package utils so you can deploy it with pip or something similar... A bit more involved at first but much easier to maintain in the long run.

这篇关于重复使用带有不同脚本的模块(代码组织)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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