5
5
----------
6
6
问题
7
7
----------
8
- You have a program that has grown beyond a simple script into an application involving
9
- multiple files. You’d like to have some easy way for users to run the program.
8
+ 您有已经一个复杂的脚本到涉及多个文件的应用程序。你想有一些简单的方法让用户运行程序。
10
9
11
10
|
12
11
13
12
----------
14
13
解决方案
15
14
----------
16
- If your application program has grown into multiple files, you can put it into its own
17
- directory and add a __main__.py file. For example, you can create a directory like this:
15
+ 如果你的应用程序已经有多个文件,你可以把你的应用程序放进它自己的目录并添加一个__main__.py文件。 举个例子,你可以像这样创建目录:
18
16
19
17
.. code-block :: python
20
18
@@ -24,16 +22,15 @@ directory and add a __main__.py file. For example, you can create a directory li
24
22
grok.py
25
23
__main__.py
26
24
27
- If __main__.py is present, you can simply run the Python interpreter on the top-level
28
- directory like this:
25
+ 如果__main__.py存在,你可以简单地在顶级目录运行Python解释器:
29
26
30
27
.. code-block :: python
31
28
32
29
bash % python3 myapplication
33
30
34
- The interpreter will execute the __main__.py file as the main program.
31
+ 解释器将执行__main__.py文件作为主程序。
35
32
36
- This technique also works if you package all of your code up into a zip file. For example:
33
+ 如果你将你的代码打包成zip文件,这种技术同样也适用,举个例子:
37
34
38
35
.. code-block :: python
39
36
@@ -48,15 +45,11 @@ This technique also works if you package all of your code up into a zip file. Fo
48
45
----------
49
46
讨论
50
47
----------
51
- Creating a directory or zip file and adding a __main__.py file is one possible way to
52
- package a larger Python application. It’s a little bit different than a package in that the
53
- code isn’t meant to be used as a standard library module that’s installed into the Python
54
- library. Instead, it’s just this bundle of code that you want to hand someone to execute.
48
+ 创建一个目录或zip文件并添加__main__.py文件来将一个更大的Python应用打包是可行的。这和作为标准库被安装到Python库的代码包是有一点区别的。相反,这只是让别人执行的代码包。
55
49
56
50
57
- Since directories and zip files are a little different than normal files, you may also want
58
- to add a supporting shell script to make execution easier. For example, if the code was
59
- in a file named myapp.zip, you could make a top-level script like this:
51
+ 由于目录和zip文件与正常文件有一点不同,你可能还需要增加一个shell脚本,使执行更加容易。例如,如果代码文件名为myapp.zip,你可以创建这样一个顶级脚本:
52
+
60
53
61
54
.. code-block :: bash
62
55
0 commit comments