How to run a Python project using __pycache__ folder?

后端 未结 1 1609
别那么骄傲
别那么骄傲 2021-01-03 04:36

I want to run a Pythonic project using Python compilation (.pyc or __pycache__). In order to do that in Python2, I haven\'t any problem.

相关标签:
1条回答
  • 2021-01-03 04:47

    You can enforce the same layout of pyc-files in the folders as in Python2 by using:

    python3 -m compileall -b test3
    

    The option -b triggers the output of pyc-files to their legacy-locations (i.e. the same as in Python2).

    After that you can once again use the compiled cache via:

    python3 main.pyc
    

    The way the loading of modules works since PEP-3147, it is impossible to use pyc-files from __pycache__ folder in the way you intend: If there is no *.py-file, the content of the __pycache__ is never looked-up. Here is the most important part of the workflow:

       import foo
         |
         |
         -- >  [foo.py exists?]  --- NO ----> [foo.pyc exists?]  -- NO --> [ImportError] 
                |                                     |
                |                                    YES
               YES                                    |--> [load foo.pyc]
                |
                |-> [look up in __pycache__]
    
                                   
    

    That means, files from __pycache__ are only looked up, when a corresponding *.py-file can be found.


    Obviously, building python scripts with a Python version 3.X in this way and trying to run the resulting pyc-files with another Python version 3.Y will not work: Different Python versions need different pyc-files, this is the whole point behind PEP-3147.

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