print('jo')
Posts: 8
Joined: Sat Nov 09, 2019 11:54 am

How to compile multiple python programs into a program

Sat Nov 09, 2019 12:06 pm

I have no idea how to say it or what its called, im just wondering.
When i sometimes download a program from github, its in multiple c, or python or some other programming language files. im wondering how to 'put these files together' if thats what you call it.

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: How to compile multiple python programs into a program

Sat Nov 09, 2019 2:12 pm

Normally you use the "import" statement. e.g.

Code: Select all

import time
import RPi.GPIO as GPIO
See https://docs.python.org/3/reference/sim ... tml#import

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to compile multiple python programs into a program

Sat Nov 09, 2019 2:21 pm

print('jo') wrote:
Sat Nov 09, 2019 12:06 pm
I have no idea how to say it or what its called, im just wondering.
When i sometimes download a program from github, its in multiple c, or python or some other programming language files. im wondering how to 'put these files together' if thats what you call it.

There is no single rule or method that applies reliably to all programming languages and GitHub contributions. Certainly C and Python are handled completely differently - a python script is interpreted, not compiled.

In general, always read what the developer/maintainer has to say about their own contributed code.

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to compile multiple python programs into a program

Sat Nov 09, 2019 2:48 pm

print('jo'),
im wondering how to 'put these files together' if thats what you call it.
Programs written in compiled languages like C, C++ Pascal, are often written as multiple source files. Each of those source files is compiled in to into machine instructions in "object" files in some binary format. Those object files are then joined together in a process known as "linking" to produce the final executable binary file.

Interpreted languages like Python, Perl, Javascript etc can also be composed of many "script" files. In that case when you run the main script file it may well reference and use some other script file. There will generally be some kind of "import" statement in a script file that causes it to make use of another one.

Generally one does not put multiple complete programs together. They are already finished programs. You might want to extract some files from one and include them in your own project.

You can build programs using multiple files in different languages, using C code from Python for example. That will be harder to varying degrees depending on which languages are involved.

Fear not, when you learn the language of your choice the details of all this will be part of the learning.
Memory in C++ is a leaky abstraction .

rushabh92
Posts: 2
Joined: Sun Nov 10, 2019 1:54 pm

Re: How to compile multiple python programs into a program

Sun Nov 10, 2019 1:59 pm

You can compile the multiple programs in Python.

However, if you do not want to create multiple programs then you can go for module wise approach.

In python, you can create your own module and then reuse that module anywhere in your project.

See the following reference link for more information about Python modules.

https://appdividend.com/2019/02/05/pyth ... in-python/

sid_snake
Posts: 11
Joined: Thu Sep 05, 2019 8:04 pm

Re: How to compile multiple python programs into a program

Wed Nov 13, 2019 9:00 am

This is a good question, something I was pondering over myself. Thanks rushabh92 your weblink explains the matter very well. I can see this Python language is really quite good, modules are the way to go.
I was writing my Python code in mini-programs or modules with the view of linking them together somehow, now I know how.

Personally though I'd not consider trying to link in C code or anything other than Python, as that requires a lot more knowledge.

icetutor
Posts: 1
Joined: Wed Feb 26, 2020 4:48 pm

Re: How to compile multiple python programs into a program

Wed Feb 26, 2020 4:50 pm

Normally you use the "import" statement. e.g.
Code: Select all

import time
import RPi.GPIO as GPIO
See http://icetutor.com/

Eyssant
Posts: 2
Joined: Sat Nov 30, 2019 6:18 am
Contact: Website

Re: How to compile multiple python programs into a program

Wed Apr 29, 2020 8:43 am

You have to import module in the current session.

Code: Select all

#import module
import ModuleName

#use object of a module
ModuleName.ObjectName(parameters)
In the below example, the math module is imported in the current session. The math module contains an object called pi which is used in the current session to calculate the area of a circle.

Code: Select all

import math

radius = 1
pi = math.pi
area = pi*(radius)**2
print(area)
Ouptut will be:

Code: Select all

3.1416

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to compile multiple python programs into a program

Wed Apr 29, 2020 8:57 am

print('jo') wrote:
Sat Nov 09, 2019 12:06 pm
I have no idea how to say it or what its called, im just wondering.
When i sometimes download a program from github, its in multiple c, or python or some other programming language files. im wondering how to 'put these files together' if thats what you call it.
Python is interpreted. There's no compiling involved (except to bytecode to make it run quicker). Type it in, run it, edit the bugs out, run it (rinse & repeat).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”