How to build a single python file from multiple scripts?

前端 未结 8 1788
终归单人心
终归单人心 2020-12-24 01:30

I have a simple python script, which imports various other modules I\'ve written (and so on). Due to my environment, my PYTHONPATH is quite long. I\'m also using Python 2.

8条回答
  •  难免孤独
    2020-12-24 02:13

    The only way to send a single .py is if the code from all of the various modules were moved into the single script and they your'd have to redo everything to reference the new locations.

    A better way of doing it would be to move the modules in question into subdirectories under the same directory as your command. You can then make sure that the subdirectory containing the module has a __init__.py that imports the primary module file. At that point you can then reference things through it.

    For example:

    App Directory: /test

    Module Directory: /test/hello

    /test/hello/__init__.py contents:

    import sayhello
    

    /test/hello/sayhello.py contents:

    def print_hello():
        print 'hello!'
    

    /test/test.py contents:

    #!/usr/bin/python2.7
    
    import hello
    
    hello.sayhello.print_hello()
    

    If you run /test/test.py you will see that it runs the print_hello function from the module directory under the existing directory, no changes to your PYTHONPATH required.

提交回复
热议问题