2024-09-25 10:26:22 发布
网友
我知道当Python脚本导入到其他Python脚本中时,会创建一个.pyc脚本。有没有其他方法可以使用LinuxBash终端创建.pyc文件?
使用以下命令:
python -m compileall <your_script.py>
这将在同一目录中创建your_script.pyc文件。
your_script.pyc
您还可以将目录传递为:
python -m compileall <directory>
这将为目录中的所有.py文件创建.pyc文件
另一种方法是创建另一个脚本
import py_compile py_compile.compile("your_script.py")
它还会创建your_script.pyc文件。可以将文件名作为命令行参数
您可以使用^{}模块。从命令行(-m选项)运行它:
-m
When this module is run as a script, the main() is used to compile all the files named on the command line.
示例:
$ tree . └── script.py 0 directories, 1 file $ python3 -mpy_compile script.py $ tree . ├── __pycache__ │ └── script.cpython-34.pyc └── script.py 1 directory, 2 files
^{}提供了类似的功能,要使用它,您可以执行如下操作
$ python3 -m compileall ...
其中...是要编译的文件或包含源文件的目录,递归遍历。
...
另一个选项是导入模块:
$ tree . ├── module.py ├── __pycache__ │ └── script.cpython-34.pyc └── script.py 1 directory, 3 files $ python3 -c 'import module' $ tree . ├── module.py ├── __pycache__ │ ├── module.cpython-34.pyc │ └── script.cpython-34.pyc └── script.py 1 directory, 4 files
-c 'import module'与-m module不同,因为前者不会在module.py中执行if __name__ == '__main__':块。
-c 'import module'
-m module
if __name__ == '__main__':
^{}提供了类似的功能,要使用它,您需要执行如下操作
使用以下命令:
这将在同一目录中创建
your_script.pyc
文件。您还可以将目录传递为:
这将为目录中的所有.py文件创建.pyc文件
另一种方法是创建另一个脚本
它还会创建your_script.pyc文件。可以将文件名作为命令行参数
您可以使用^{} 模块。从命令行(
-m
选项)运行它:示例:
^{} 提供了类似的功能,要使用它,您可以执行如下操作
其中
...
是要编译的文件或包含源文件的目录,递归遍历。另一个选项是导入模块:
-c 'import module'
与-m module
不同,因为前者不会在module.py中执行if __name__ == '__main__':
块。您可以使用^{} 模块。从命令行(
-m
选项)运行它:示例:
^{} 提供了类似的功能,要使用它,您需要执行如下操作
其中
...
是要编译的文件或包含源文件的目录,递归遍历。另一个选项是导入模块:
-c 'import module'
与-m module
不同,因为前者不会在module.py中执行if __name__ == '__main__':
块。相关问题 更多 >
编程相关推荐