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?相关推荐

  1. python程序移植到linux_如何将Python自编软件装到Linux系统

    用 Python 编写的软件是如何安装到 Linux 系统中的? 用 C 或 C++ 编写的软件可以通过使用 make install 来安装的系统之中.用 Python 写的软件又该如何安装? == ...

  2. python 程序运行插件_如何使Python插件在Pluma中运行?

    我正在尝试将Python插件trailave添加到文本编辑器Pluma *(这是一个Gedit fork),但该插件没有显示在Pluma首选项的"Active plugins"列表 ...

  3. Python程序设计基础第一章笔记:Python概述

    Python程序设计基础笔记 目录 Python程序设计基础笔记 第一章:python概述 1.1 python是这样一种语言 1.2 python版本之争 略 1.3 python编程规范与代码优化 ...

  4. 代写python期末作业价格_代写program留学生作业、代做Python程序语言作业、代写Python课程设计作业...

    代写program留学生作业.代做Python程序语言作业.代写Python课程设计作业 日期:2019-11-29 12:55 Completing the Final Project - Pyth ...

  5. 树莓派能直接运行python程序_在树莓派上运行python脚本 - python

    我正在研究rapiberry pi 3大约3个月,开始使用它时遇到了问题. 我找不到在树莓派打开时在其上运行python脚本的有效,安全的方法(没有监视器,鼠标和键盘).此刻,我在/ etc / pr ...

  6. 缩进对于python程序至关重要吗_缩进对于Python程序至关重要。

    [判断题]用截面法求内力时,可以保留截开后构件的任意部分进行平衡计算 . [单选题]对大量生产的零件,机加工应采用( ) [单选题]对事件重要与紧急情况的分析,也是对: [单选题]在下列传动中,平均传 ...

  7. python程序员工作怎样-12个Python程序员面试必备问题与答案(小结)

    1. 什么是pickling和unpickling? Pickle模块读入任何Python对象,将它们转换成字符串,然后使用dump函数将其转储到一个文件中――这个过程叫做pickling.反之从存储 ...

  8. 温度转换的python程序_人生苦短,请用Python!

    想必大家听过这么一句话: "人生苦短,我用Python!" 其实这句关于python的流行梗的原话是: Life is short. You need Python. --布鲁斯· ...

  9. 执行 Python 程序的三种方式及Python 的 IDE —— `PyCharm`

    执行 Python 程序的三种方式 3.1. 解释器 python / python3 Python 的解释器 # 使用 python 2.x 解释器 $ python xxx.py# 使用 pyth ...

最新文章

  1. 我是如何设计 Upload 上传组件的
  2. Hyper-V 网卡 load failed
  3. python制表符什么意思_python中制表符是什么意思
  4. mysql5.7 事件_MySQL 5.7新特性
  5. SQL Server 审计
  6. java基础排序_Java排序算法-Java入门|Java基础课程
  7. windows进入mysql改user_windows下如何修改mysql数据库密码
  8. 张家口以太坊智能合约开发实战pdf_以太坊2.0:实现可扩展性之路漫长而曲折
  9. 用php向MySQL的datetime类型插入数据
  10. 生产环境mysql主主同步主键冲突处理
  11. linux 查找html文件,linux查找文件内容包含
  12. 广东英语高考怎么计算机,2019广东高考英语听说考试大纲出炉!附三大题型得分套路!...
  13. Python3字典合并的几种方法
  14. 歌曲计算机教案反思,幼儿园音乐公开课教案及反思三篇
  15. fatal error: opencv2\core\core.hpp: No such file or directory
  16. ajax异步登陆 cookie,通过ajax登录到远程系统并设置Cookie
  17. Linux 学习笔记——二、主机规划与磁盘分区
  18. (转)写给找工作的朋友们
  19. mysql 存储时间应该是时间戳还是时间_3分钟短文 | MySQL存时间,到底该用timestamp还是datetime?...
  20. golang包time用法详解

热门文章

  1. BZOJ2038: [2009国家集训队]小Z的袜子(hose)
  2. gulp前端构建化工具,帮你搞定不同浏览器的兼容性写法问题
  3. iOS-可变参数(不定参数)的用法
  4. 不错的博 2010-02-07
  5. Use Case 和User Story
  6. BZOJ.4821.[SDOI2017]相关分析(线段树)
  7. C博客作业03--函数
  8. python_魔法方法(六):迭代器和生成器
  9. BZOJ4567 SCOI2016背单词(trie+贪心)
  10. 使用JDBC操作数据库时,如何提升读取数据的性能?如何提升更新数据的性能?...