简体   繁体   English

正确的方式来组织python项目结构和分发

[英]Right way to organize python project structure and distributing

I have a Python client-service project. 我有一个Python客户服务项目。 Logical dependencies look like this: 逻辑依赖性如下所示: 逻辑依存关系

Say in words, I have some services ( srv1 , srv2 , ..., srvX ) with common code in service.py , some clients for these services ( srv1_cl.py , srv2_cl.py , ..., srvX_cl.py ) with common code in client.py and common code for both service and clients in common.py . 简而言之,我在service.py中有一些具有通用代码的服务( srv1srv2 ,..., srvX ),这些服务的一些客户端( srv1_cl.pysrv2_cl.py ,..., srvX_cl.py )具有client.py通用代码和common.py服务和客户端的通用代码。

Now I have problem with structuring my git repository for development and deploying my project to users. 现在,我在构建用于开发的git存储库以及将项目部署到用户方面遇到问题。

Firstly I tried to store my clients and service in separate folders. 首先,我尝试将客户和服务存储在单独的文件夹中。 Moreover, for each service I created a folder, because each service can contain a lot of files. 而且,对于每个服务,我创建了一个文件夹,因为每个服务可以包含很多文件。

/myrepo.git
    common.py
    /clients
        client.py
        srv1_cl.py
        srv2_cl.py
    /services
        service.py
        /srv1
            conf.py
            srv1.py
            start.bat
        /srv2
            conf.py
            srv2.py

When I store my files like that I ran into a problem - how can a Python script export files from upper directories? 当我这样存储文件时,我遇到了一个问题-Python脚本如何从上层目录中导出文件? Because I need test my client scripts, make some changes and execute. 由于我需要测试客户端脚本,因此请进行一些更改并执行。

I found some solutions based on adding PATH with upper folders ( sys.path.insert(0, parentdir) , but I'm sure it's not a good way) and relative imports . 我发现了一些基于将PATH添加到较高文件夹( sys.path.insert(0, parentdir)解决方案,但是我确定这不是一个好方法)和相对导入 But for relative imports I must wrap my scripts into one package. 但是对于相对导入,我必须将我的脚本包装到一个包中。

OK, let's say I will do this, but here's another problem. 好的,假设我会这样做,但这是另一个问题。 I want to deploy "clients" separately from "services" because I don't want share the services' code with my users. 我想与“服务”分开部署“客户端”,因为我不想与用户共享服务的代码。 How can I handle this problem? 我该如何解决这个问题?

What is the best structure for my project? 我的项目的最佳结构是什么? How can I develop and debug my project when I wrap my code to a package? 将代码包装到程序包中时,如何开发和调试项目? Because for testing I want to start clients from my subfolders. 因为要进行测试,所以我想从子文件夹启动客户端。

Good question. 好问题。 I ran into a similar problem with importing modules at different levels in the directory tree, and deployment. 在目录树的不同级别导入模块并进行部署时,我遇到了类似的问题。 I ended up up reorganizing the project tree to be very flat(all files in one directory) and just naming the files informativeley, and refractoring code a lot to get the desired functionality. 我最终将项目树重组为非常扁平的(所有文件都在一个目录中),并仅命名文件信息,然后大量重新整理代码以获得所需的功能。 And the for deployment, I copied the main pyc file into the subproject folder. 为了进行部署,我将主要的pyc文件复制到了子项目文件夹中。 I would love to know a better way though. 我很想知道更好的方法。

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

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