Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写在Unix和Windows上运行的简单python脚本的最佳方法是什么_Python_Windows_Github - Fatal编程技术网

编写在Unix和Windows上运行的简单python脚本的最佳方法是什么

编写在Unix和Windows上运行的简单python脚本的最佳方法是什么,python,windows,github,Python,Windows,Github,我已经用Python编写了一个基本的internet速度检查应用程序(3)。最初我编写它是为了通过任务调度器在我的Windows机器上运行。然后我将它复制到我的Pi(运行Raspian)中,并通过Cron编辑它。我想增强代码,但仍然能够在Windows或Unix下运行它。我还想开始使用github。因此,最好的方法是在github中维护两个独立的应用程序,一个应用程序,但分叉或分支2版本,或者在它们的基础上做一个变化,如果os=win,那么xxxx;如果os=unix,那么。。。 希望这是有道理

我已经用Python编写了一个基本的internet速度检查应用程序(3)。最初我编写它是为了通过任务调度器在我的Windows机器上运行。然后我将它复制到我的Pi(运行Raspian)中,并通过Cron编辑它。我想增强代码,但仍然能够在Windows或Unix下运行它。我还想开始使用github。因此,最好的方法是在github中维护两个独立的应用程序,一个应用程序,但分叉或分支2版本,或者在它们的基础上做一个变化,如果os=win,那么xxxx;如果os=unix,那么。。。 希望这是有道理的。谢谢


我目前有两个独立的代码库,它们非常相似,但有差异(例如对目录/文件的引用等)。

当然是同一个项目。在
main
中,将公共代码提取到基类中(并尽可能多地生成公共代码),决定调用哪个平台(
import platform;platform.system().lower()
将为您提供简单的“linux”和“windows”值),然后从该特定类创建实例。如下所示:

# myapp/base.py

class BaseApp(object):

# myapp/windows/app.py and myapp/linux/app.py

class App(BaseApp):

伟大的感谢您的快速回答和代码示例。抱歉一件事-大体上-如果是UNIX,我需要在一开始就添加shebang-但由于需要首先添加shebang,所以我必须在确定是UNIX还是Windows之前添加它。shebang不会干扰Windows中的执行,如果您愿意,只需在那里添加即可。