Compiling Python

前端 未结 8 765
故里飘歌
故里飘歌 2020-12-29 17:59

How can I compile and run a python file (*.py extension)?

相关标签:
8条回答
  • 2020-12-29 18:50

    If you just want to compile sources, without running them, you can do this

    compileall.py <directory>
    

    this command will compile python code in that directory recursively

    compileall script is usually located in directory like

    /usr/local/lib/python2.6
    

    i.e. <prefix>/lib/python2.6 (or similar, depending on prefixes set a python configuration)

    As Lulu suggests, you should make sure that resulting .pyc and .pyo files are executable by the users you care about.

    compileall can also be used as a module

    import compileall
    compileall.compile_dir(path)
    
    0 讨论(0)
  • 2020-12-29 18:51

    To add to Paul McMillan's answer, if you are on Windows and you have Python installed, then any files ending with the extension ".py" should be associated with the python executable, allowing you to run it like so:

    > myfile.py
    

    In *nix, you can begin the file with #!/usr/bin/python and run it like so:

    $ ./myfile.py
    

    In *nix systems, if the first two characters of a file are #! then it will execute the file with the specified executable, which I set here to be /usr/bin/python.

    0 讨论(0)
提交回复
热议问题