0

In C/C++ we write a makefile for a project installation/execution. How do we create a makefile or equivalent for a python project?

If python is a scripting language and we don't need a makefile. How do we integrate a python project with multiple python files (.py files)?

There is another thread with a similar question 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.

thesukantadey
  • 97
  • 1
  • 7
  • 1
    python is a scripting language, there's no need for a makefile. –  Feb 01 '18 at 22:21
  • @TheDude If python is a scripting language and we don't need a makefile. How do we integrate a python project with multiple python files (.py files)? – thesukantadey Feb 01 '18 at 22:37
  • https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python –  Feb 01 '18 at 22:42
  • 1
    Possible duplicate of [Call a function from another file in Python](https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python) – Mr. T Feb 02 '18 at 00:39
  • 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. – thesukantadey Feb 02 '18 at 09:20

4 Answers4

2

The c/c++ makefile is mainly used for compiling the project, which is not needed with python as it is a a script language (not compiled). What kind of operations do you require from your makefile? For package management you can read about pip (a common python package manager)

Gal Bashan
  • 112
  • 6
  • If python is a scripting language and we don't need a makefile. How do we integrate a python project with multiple python files (.py files)? – thesukantadey Feb 01 '18 at 22:38
  • What do you mean by integrate? You usually have a main/some other entry point, which uses the other modules (import them). There is no need for any other integration but that. Maybe you can share your code example so the question would be clearer – Gal Bashan Feb 02 '18 at 09:41
2

Python is a scripting language this means that there is no compiling/recompiling is necessary and errors are reported when you run the script. This removes one of the greatest assets of using make.

Although you can use makefiles for replacing long commands in the command line or controlling dictionaries/files, it is unlikely you will really need this.

If you are using C/C++ in with your Python project then it is highly recommendable.


You mention intergrating. makefiles are unlikely to be the tools you want. You need to build a module at the very least.

Modules allow you to use functions from other python files as if they were in that file. You need to import them and that's pretty much it.

If you want to install on other PCs use setup.py script to create a package. This allows you to make your project installable and then the project can be used just like an extension to Python.


Python is not like C or C++. Just knowing where to find the files together is fine for Python and when they are turned into modules you will just have to import them once and you will be able to use the functions they provide

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • makefiles are not useless in Python or any other language. `make` is a tool, and you can run makefiles with recipes to automate frequent tasks on the command line, in many scenarios. I agree with you that of course, you aren't in this case building a target by compiling files. – progmatico Feb 01 '18 at 22:26
  • @progmatico I will edit please give me a minute – Xantium Feb 01 '18 at 22:34
  • @Simon If python is a scripting language and we don't need a makefile. How do we integrate a python project with multiple python files (.py files)? – thesukantadey Feb 01 '18 at 22:36
  • @thesukantadey See my updated answer. If you have further questions please say so. – Xantium Feb 01 '18 at 23:06
2

Arrange your project of several python files into a package. Package can mean to put several files together for others to use your code by means of an import package, or actually to distribute your app for others to use.

For the first just create a folder with the py files in it, and an empty __init__.py inside that folder. For the first and the second scenario also read the documentation here, and maybe here too.

progmatico
  • 4,714
  • 1
  • 16
  • 27
0

The best analog is a shell script that executes your file. It could include taking care of any complicated arguments, setting environmental variables if necessary. To include other .py files, you can probably just import them into your main python file. But if you have more advanced needs I would recommend a second question

3pitt
  • 899
  • 13
  • 21