What is the proper way to work with shared modules in Python development?

后端 未结 4 1501
借酒劲吻你
借酒劲吻你 2020-12-12 21:59

I\'m working toward adopting Python as part of my team\'s development tool suite. With the other languages/tools we use, we develop many reusable functions and classes that

4条回答
  •  醉话见心
    2020-12-12 22:32

    ...it seems that there is an assumption that all referenced packages are below the top-level project folder, not collateral to it.

    That's mainly because the current working directory is the first entry in sys.path by default, which makes it very convenient to import modules and packages below that directory.

    If you remove it, you can't even import stuff from the current working directory...

    $ touch foo.py
    $ python
    >>> import sys
    >>> del sys.path[0]
    >>> import foo
    Traceback (most recent call last):
      File "", line 1, in 
    ImportError: No module named foo
    

    The thought also occurred to me that perhaps the correct approach is to develop common/framework modules in a separate project, and once tested, deploy those to each developer's environment by installing to the site-packages folder.

    It's not really a major issue for development. If you're using version control, and all developers check out the source tree in the same structure, you can easily employ relative path hacks to ensure the code works correctly without having to mess around with environment variables or symbolic links.

    However, that also raises questions re distribution.

    This is where things can get a bit more complicated, but only if you're planning to release libraries independently of the projects which use them, and/or having multiple project installers share the same libraries. It that's the case, take a look at distutils.

    If not, you can simply employ the same relative path hacks used in development to ensure you project works "out of the box".

提交回复
热议问题