简体   繁体   English

为现有项目结构创建 setup.py

[英]Creating setup.py for existing project structure

I started working on a project with the following folder structure where each top folder lives in a separate repository:我开始研究一个具有以下文件夹结构的项目,其中每个顶级文件夹都位于一个单独的存储库中:

project-api/
  source/
     module1.py
     module2.py
project-core/
  source/
     module3.py
     module4.py

I would like to pip install -e and be able to do:我想pip install -e并且能够执行以下操作:

from api.module1 import function1
from core.module3 import function3

without changing the folder structure (fixed by the project).不改变文件夹结构(由项目固定)。

Question : How can I create the corresponding(s) setup.py ?问题:如何创建相应的setup.py

You cannot do that with pip install -e because option -e "installs" packages in development/editable mode.您不能使用pip install -e执行此操作,因为选项-e在开发/可编辑模式下“安装”软件包。 It doesn't actually install anything but creates a link that allows Python to import modules directly from the development directory.它实际上并不安装任何东西,而是创建一个链接,允许 Python 直接从开发目录导入模块。 Unfortunately in your case that impossible — directories project-core and project-api contain forbidden character in their name ( - ) — these directories cannot be made importable in-place.不幸的是,在您的情况下这是不可能的 - 目录project-coreproject-api在其名称中包含禁止字符( - ) - 这些目录无法就地导入。

But pip install .但是pip install . could be made to install top-level packages api and core from these directories.可以从这些目录安装顶级包apicore First you have to add __init__.py :首先你必须添加__init__.py

touch project-api/source/__init__.py
touch project-core/source/__init__.py

And the following setup.py do the rest:下面的setup.py完成剩下的工作:

#!/usr/bin/env python

from setuptools import setup

setup(
    name='example',
    version='0.0.1',
    description='Example',
    packages=['api', 'core'],
    package_dir={
        'api': 'project-api/source',
        'core': 'project-core/source',
    }
)

Run pip install .运行pip install . and you're done.你就完成了。 Execute import api, core in Python.执行import api, core在 Python 中。

PS.附注。 If you can create symlinks at the root:如果您可以在根创建符号链接:

ln -s project-api/source api
ln -s project-core/source core

you can use pip install -e .您可以使用pip install -e .

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

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