如何在单独的文件夹中编译python文件? [英] Way to have compiled python files in a separate folder?
问题描述
是否可以让Python将.pyc
文件保存到sys.path
中的单独文件夹位置?
Is it possible to have Python save the .pyc
files to a separate folder location that is in sys.path
?
/code
foo.py
foo.pyc
bar.py
bar.pyc
收件人:
/code
foo.py
bar.py
/code_compiled
foo.pyc
bar.pyc
我想要这个,因为我觉得它会更有条理.谢谢您能给我的帮助.
I would like this because I feel it'd be more organized. Thanks for any help you can give me.
推荐答案
更新:
在Python 3.8中,-X pycache_prefix=PATH
命令行选项允许将.pyc
文件写入到以给定目录为根的并行树中,而不是代码树中.请参见 $PYTHONPYCACHEPREFIX
envvar 信用: @RobertT'答案
In Python 3.8 -X pycache_prefix=PATH
command-line option enables writing .pyc
files to a parallel tree rooted at the given directory instead of to the code tree. See $PYTHONPYCACHEPREFIX
envvarcredits: @RobertT' answer
在 sys.pycache_prefix
(None
表示__pycache__
[因为Python 3.2]子目录中的默认位置).
The location of the cache is reported in sys.pycache_prefix
(None
indicates the default location in __pycache__
[since Python 3.2] subdirectories).
要关闭对已编译的Python字节码的缓存,可以设置-B
,这样Python不会尝试在导入源模块时写入.pyc
文件.请参见 $PYTHONDONTWRITEBYTECODE
envvar 信用: @Maleev的答案
To turn off caching the compiled Python bytecode, -B
may be set, then Python won’t try to write .pyc
files on the import of source modules. See $PYTHONDONTWRITEBYTECODE
envvarcredits: @Maleev's answer
旧的[Python 2]答案:
有 PEP 304:控制字节码文件的生成.其状态为Withdrawn
,相应的补丁被拒绝.因此,可能没有直接的方法可以做到这一点.
There is PEP 304: Controlling Generation of Bytecode Files. Its status is Withdrawn
and corresponding patch rejected. Therefore there might be no direct way to do it.
如果不需要源代码,则可以删除*.py
文件. *.pyc
文件可以直接使用,也可以装在鸡蛋中.
If you don't need source code then you may just delete *.py
files. *.pyc
files can be used as is or packed in an egg.
这篇关于如何在单独的文件夹中编译python文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!