如何告诉Python在同时存在.py和.pyc文件时运行.py文件?

huangapple go评论65阅读模式
英文:

How to tell python to run a .py file when both a .pyc and .py file are present?

问题

如何告诉Python在文件夹中同时存在.py.pyc文件时使用.py文件?

英文:

I have a project that I have compiled into .pyc files for security, but I want to be able to drop a .py file into my project for debugging purposes as necessary. How can I tell python to use a .py file if both the .py and .pyc files are present in a folder?

i.e. I have:

main:
   |
   run.pyc
   /src/
      |
      dependency1.pyc
      dependency2.pyc
      dependency2.py
      dependency3.pyc

and when both dependency2.pyc and dependency2.py are in /src/ I want run.pyc to use dependency2.py.

答案1

得分: 1

关于调试。

根据Python文档:

当源文件与一个遗留的pyc文件并存时,Python会忽略该pyc文件。换句话说,如果在foo.py文件旁边存在一个foo.pyc文件,那么在所有情况下都会忽略pyc文件。

在你的情况下,run.py 应该使用 dependency2.py 并忽略相关的 pyc 文件,这将允许你进行调试。

如果这不起作用,尝试暂时将编译后的文件 dependency2.pyc 移出目录。

英文:

With respect to debugging.

From Python docs:
> Python will ignore all legacy pyc files when a source file exists next to it. In other words, if a foo.pyc file exists next to the foo.py file, the pyc file will be ignored in all cases

In your case, run.py should be using dependency2.py and ignoring the associated pyc, which in turn should allow you to debug.

If that's not happening, try to temporarily move the compiled file dependency2.pyc out of the directory.

huangapple
  • 本文由 发表于 2023年6月19日 22:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定