python import path: packages with the same name in different folders

前端 未结 3 1778
天命终不由人
天命终不由人 2020-12-24 14:15

I am developing several Python projects for several customers at the same time. A simplified version of my project folder structure looks something like this:



        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 14:47

    What happens if you accidentally import code from one customer/project in another and don't notice? When you deliver it will almost certainly fail. I would adopt a convention of having PYTHONPATH set up for one project at a time, and not try to have everything you've ever written be importable at once.

    You can use a wrapper script per-project to set PYTHONPATH and start python, or use scripts to switch environments when you switch projects.

    Of course some projects well have dependencies on other projects (those libraries you mentioned), but if you intend for the customer to be able to import several projects at once then you have to arrange for the names to not clash. You can only have this problem when you have multiple projects on the PYTHONPATH that aren't supposed to be used together.

提交回复
热议问题