gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:46:53 28 4
gpt4 key购买 nike

主文件

import other

def main():
other.test()

main()

其他.py
def test():
print("Hello")

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

然而, main.pyc如果存在 no module named other 则无法运行,这是我从终端得到的错误。

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

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

最佳答案

问了一个机器学习小组。这是我发现的。
只要,main.pyother.py编译为 main.pycother.pyc ,我可以通过python3 main.pyc运行它.

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

因此,请确保 .pyc文件与 .py 同名,然后你可以运行它们中的任何一个,python 将包含 .pyc执行命令时为您提供文件。

关于python - 如何在导入其他一些 .py 文件时运行 .pyc 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46783245/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com