如何在导入其他.py文件时运行.pyc文件?

时间:2017-10-17 05:46:37

标签: python pyc

main.py

import other

def main():
   other.test()

main()

other.py

def test():
   print("Hello")

使用python3 -m py_compile *.py,我可以拥有2个.pyc文件。

但是,main.pyc如果有no module named other则无法运行,这是我从终端获得的错误。

我们的想法是将整个项目从.py编译到.pyc,这样人们就可以在不共享源代码的情况下运行它们。

那么,如何运行导入其他库的main.pyc,而不共享源代码?

2 个答案:

答案 0 :(得分:1)

问一个机器学习小组。这是我发现的。 只要将ValueError: logits and labels must have the same shape ((64, 1) vs (128, 1)) main.py编译为other.pymain.pyc,我就可以other.pyc运行它。

在此之前,我的python会自动将python3 main.pyc转换为other.py。在这种情况下,other.cpython-35.pyc无法main.pyc,因为文件夹中没有import other(现在称为other)。

因此,请确保other.cpython-35文件与.pyc具有相同的名称,然后您可以运行其中任何一个文件,并且在执行命令时python将包含.py文件。

答案 1 :(得分:1)

这也可以通过以下命令来实现:

python -m compileall -b .