如何为不同项目放置通用代码 [英] How to place a common code for different projects

查看:130
本文介绍了如何为不同项目放置通用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单独的文件夹中有两个python项目。而且我在两个项目中都使用了一个通用代码。

I've got two python projects in a separate folders. And I also have a common code which I use in both of my projects.

在文件系统上对其进行组织以将distutils与setup.py结合使用的最佳方法是什么?

What is the best way to organize to them on filesystem to use distutils with setup.py to build them?

我的第一个想法是使用这样的结构:

My first idea was to use the structure like this:

workspace
    |
    |- common lib
    |    |
    |    `- ...
    |
    |- project 1
    |    |
    |    |- ...
    |    `- setup.py
    |
    `- project 2
         |
         |- ...
         `- setup.py

并修改在项目中的python路径环境变量中找到代码,但是在编写setup.py文件时遇到了问题。如果我为我的公共包(..\common lib)选择了相对路径,则distutils将公共lib文件放在存档之外。

and modify the python path environment variable in projects to find the code, but I've run into problems when I write setup.py file. If I choose the relative path for my common package (..\common lib), then distutils put the common lib files outside of the archive.

有什么好处吗?尝试同时放置可重用的代码并构建项目?

Is there any good practice to place reusable code and build projects at the same time?

谢谢!

推荐答案

distutils / setup-tools 中的 package_dir 选项可以插入软件包(在您的情况下, common_lib )位于分发树中与源树不同的位置( https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages )。

The package_dir option in distutils/setup-tools can insert a package (in your case, common_lib) at a location within the distribution tree that is different from the source tree (https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages).

但是据我所知,这仅在创建二进制分发版时才有效(setup.py bdist);我也在这里遇到了这个问题:行为不一致分发Python软件包时bdist与sdist的关系

But as far as I can tell this only works properly when creating a binary distribution (setup.py bdist); I ran into this problem too here: Inconsistent behaviour of bdist vs sdist when distributing a Python package.

这篇关于如何为不同项目放置通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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