简体   繁体   English

我怎样才能在python中使用通用代码?

[英]How can I use common code in python?

I'm currently maintaining two of my own applications. 我目前正在维护自己的两个应用程序。 They both share some common aspects, and as a result, share some code. 它们共享一些共同的方面,因此,共享一些代码。 So far, I've just copied the modules from one project to the other, but now it's becoming a maintenance issue. 到目前为止,我刚刚将模块从一个项目复制到另一个项目,但现在它已成为一个维护问题。 I'd rather have the common code in one place, outside of both of the projects, which they can both import. 我宁愿在两个项目之外的一个地方使用公共代码,它们都可以导入。 Then, any changes to the common code would be reflected in both project. 然后,对公共代码的任何更改都将反映在两个项目中。

My question is: how can I do this? 我的问题是:我该怎么做? Do I create a library out of this code? 我是否使用此代码创建了一个库? If so, how do the dependent projects use the library? 如果是这样,依赖项目如何使用库? I think one thing I struggle with here is that the common code isn't really useful to anyone else, or at least, I don't want to make it a supported modules that other people can use. 我认为我在这里遇到的一件事是公共代码对其他任何人都没有用,或者至少,我不想让它成为其他人可以使用的支持模块。

If my question isn't clear, please let me know. 如果我的问题不明确,请告诉我。

There is nothing special you have to do, Python just needs to find your module. 你不需要做什么特别的事情,Python只需要找到你的模块。 This means that you have to put your common module into your PYTHONPATH , or you add their location to sys.path . 这意味着您必须将公共模块放入PYTHONPATH ,或者将它们的位置添加到sys.path See this. 看到这个。

Say you have 说你有

~/python/project1
~/python/project2
~/python/libs/stuff.py
~/python/libs/other.py

You can either set PYTHONPATH='~/python/libs' in your os enviroment, or you can do 你可以在你的os环境中设置PYTHONPATH='~/python/libs' ,或者你可以做

import sys, os
sys.path.append(os.path.expanduser('~/python/libs')) # or give the full path

After that you can do import stuff, other anywhere. 之后,您可以在任何地方import stuff, other

You can also package your stuff, then you need a layout like this: 你也可以打包你的东西,然后你需要这样的布局:

~/python/project1
~/python/project2
~/python/libs/mylibname/__init__.py
~/python/libs/mylibname/stuff.py
~/python/libs/mylibname/other.py

~/python/libs/mylibname/__init__.py must exist, but it can be a empty file. ~/python/libs/mylibname/__init__.py必须存在,但它可以是一个空文件。 It turns mylibname into a package . 它将mylibname变成一个

After adding the libs folder to your path as above, you can do from mylibname import stuff, other . 将libs文件夹添加到您的路径后,您可以from mylibname import stuff, other

There are a lot of ways to factor code so it is reusable. 有很多方法可以对代码进行分解,因此它是可重用的。 It really depends on your specific situation as far as what will work best. 这取决于你的具体情况,哪种方法效果最好。 Factoring your code into separate packages and modules is always a good idea, so related code stays bundled together and can be reused from other packages and modules. 将代码分解为单独的包和模块总是一个好主意,因此相关代码保持捆绑在一起,可以从其他包和模块中重用。 Factoring your code into classes within a module can also help in keeping related code grouped together. 将代码分解为模块中的类也可以帮助将相关代码组合在一起。

I would say that putting common code into a module or package that is on your PYTHONPATH and available to both applications would probably be your best solution. 我会说将公共代码放入PYTHONPATH上的模块或包中并且可用于两个应用程序可能是您的最佳解决方案。

Here's how I would do it: 我是这样做的:

  • make an EGG archive of your common project: 制作您共同项目的EGG档案:

     ~:zip common.egg common 
  • make the egg file part of your libraries 使egg文件成为您库的一部分

     cp common.egg PROJECT_PATH/lib/ 
  • in your projects: 在你的项目中:

     import glob import os def main(): path_lib=os.path.abspath(os.path.split(os.path.abspath(sys.modules['__main__'].__file__))[0] + '/../lib') sys.path += glob.glob(path_lib + '/*.egg') from common import stuff stuff.doCommonStuff() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM