为 python 项目集成多个 python 文件的 makefile 替代品
Substitute of the makefile for integrating mulitple python files for a python project
在 C/C++ 中,我们为项目 installation/execution 编写了一个 makefile。我们如何为 python 项目创建 makefile 或等效文件?
如果python是一种脚本语言,我们不需要makefile。我们如何将一个 python 项目与多个 python 文件(.py 文件)集成?
还有一个帖子有类似的问题
Call a function from another file in Python. But my question is different from the one asked in that thread. One solution of my question may be possible by calling a function from another file. But I wanted a better solution as described by the Simon.
c/c++ makefile 主要用于编译工程,python 不需要,因为它是一种脚本语言(未编译)。你的 makefile 需要什么样的操作?对于包管理,您可以阅读 pip(一个常见的 python 包管理器)
Python 是一种脚本语言,这意味着不需要 compiling/recompiling 并且当您 运行 脚本时会报告错误。这消除了使用 make
的最大优势之一。
虽然您可以使用 makefile 替换命令行中的长命令或控制 dictionaries/files,但您不太可能真正需要它。
如果您在 Python 项目中使用 C/C++,那么强烈推荐它。
你提到集成。 makefile 不太可能是您想要的工具。你至少需要建立一个module。
模块允许您使用其他 python 文件中的函数,就像它们在该文件中一样。您需要导入它们,仅此而已。
如果您想在其他电脑上安装,请使用 setup.py script 创建一个程序包。这允许您使您的项目可安装,然后可以像 Python.
的扩展一样使用该项目
Python 不像 C 或 C++。 Python 只需知道在哪里可以找到这些文件就可以了,当它们变成模块时,您只需导入一次它们就可以使用它们提供的功能
最好的模拟是执行文件的 shell 脚本。它可能包括处理任何复杂的参数,必要时设置环境变量。要包含其他 .py 文件,您可能只需将它们导入主 python 文件即可。但是如果你有更高级的需求我会推荐第二个问题
将几个 python 文件的项目安排到一个包中。
打包可以意味着将几个文件放在一起供其他人通过import package
使用您的代码,或者实际上是分发您的应用程序供其他人使用。
首先,只需创建一个包含 py 文件的文件夹,并在该文件夹中创建一个空的 __init__.py
。对于第一个和第二个场景,也请阅读文档 here, and maybe here。
在 C/C++ 中,我们为项目 installation/execution 编写了一个 makefile。我们如何为 python 项目创建 makefile 或等效文件?
如果python是一种脚本语言,我们不需要makefile。我们如何将一个 python 项目与多个 python 文件(.py 文件)集成?
还有一个帖子有类似的问题 Call a function from another file in Python. But my question is different from the one asked in that thread. One solution of my question may be possible by calling a function from another file. But I wanted a better solution as described by the Simon.
c/c++ makefile 主要用于编译工程,python 不需要,因为它是一种脚本语言(未编译)。你的 makefile 需要什么样的操作?对于包管理,您可以阅读 pip(一个常见的 python 包管理器)
Python 是一种脚本语言,这意味着不需要 compiling/recompiling 并且当您 运行 脚本时会报告错误。这消除了使用 make
的最大优势之一。
虽然您可以使用 makefile 替换命令行中的长命令或控制 dictionaries/files,但您不太可能真正需要它。
如果您在 Python 项目中使用 C/C++,那么强烈推荐它。
你提到集成。 makefile 不太可能是您想要的工具。你至少需要建立一个module。
模块允许您使用其他 python 文件中的函数,就像它们在该文件中一样。您需要导入它们,仅此而已。
如果您想在其他电脑上安装,请使用 setup.py script 创建一个程序包。这允许您使您的项目可安装,然后可以像 Python.
的扩展一样使用该项目Python 不像 C 或 C++。 Python 只需知道在哪里可以找到这些文件就可以了,当它们变成模块时,您只需导入一次它们就可以使用它们提供的功能
最好的模拟是执行文件的 shell 脚本。它可能包括处理任何复杂的参数,必要时设置环境变量。要包含其他 .py 文件,您可能只需将它们导入主 python 文件即可。但是如果你有更高级的需求我会推荐第二个问题
将几个 python 文件的项目安排到一个包中。
打包可以意味着将几个文件放在一起供其他人通过import package
使用您的代码,或者实际上是分发您的应用程序供其他人使用。
首先,只需创建一个包含 py 文件的文件夹,并在该文件夹中创建一个空的 __init__.py
。对于第一个和第二个场景,也请阅读文档 here, and maybe here。