python程序移植到linux,如何使python或perl脚本可移植到Linux和Windows?
I was wondering how to make a python script portable to both linux and windows?
One problem I see is shebang. How to write the shebang so that the script can be run on both windows and linux?
Are there other problems besides shebang that I should know?
Is the solution same for perl script?
Thanks and regards!
解決方案
Windows will just ignore the shebang (which is, after all, a comment); in Windows you need to associate the .py extension to the Python executable in the registry, but you can perfectly well leave the shebang on, it will be perfectly innocuous there.
There are many bits and pieces which are platform-specific (many only exist on Unix, msvcrt only on Windows) so if you want to be portable you should abstain from those; some are subtly different (such as the detailed precise behavior of subprocess.Popen or mmap) -- it's all pretty advanced stuff and the docs will guide you there. If you're executing (via subprocess or otherwise) external commands you'd better make sure they exist on both platforms, of course, or check what platform you're in and use different external commands in each case.
Remember to always use /, not \, as path separator (forward slash works in both platforms, backwards slash is windows-only), and be meticulous as to whether each file you're opening is binary or text.
I think that's about it...
總結
以上是生活随笔為你收集整理的python程序移植到linux,如何使python或perl脚本可移植到Linux和Windows?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卫生统计学v是什么意思_四川大学华西公共
- 下一篇: cefsharp 网页打印不好用_201