Skip to content

Commit 1a1c38a

Browse files
committed
Add PyInstaller example
1 parent a1ba9dc commit 1a1c38a

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

source-code/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ was used to develop it.
3333
1. `code-organization`: illustration of how to organize code in packages
3434
and modules.
3535
1. `error-handling`: simple illustration of error handling.
36+
1. `pyinstaller`: illustration of how to use `pyinstaller` to create
37+
standalone executables.

source-code/pyinstaller/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# virtual environment to build applications
2+
build_env/
3+
4+
# build directory
5+
build/
6+
7+
# distribution directory
8+
dist/
9+
10+
# Python cache directories
11+
__pycache__/

source-code/pyinstaller/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PyInstaller
2+
3+
PyInstaller bundles a Python application and all its dependencies into a single
4+
package. The user can run the packaged app without installing a Python
5+
interpreter or any modules. It is very useful to minimize dependencies.
6+
7+
8+
## What is it?
9+
10+
1. `src`: directory with a Python scripts that use the pandas library and that
11+
requires access to a CSV file. Note that the Python scripts dynamically
12+
load a module `funcs` that is not in the same directory as the script.
13+
1. `data.csv`: CSV file that is used by the Python script in `src`.
14+
1. `requirements.txt`: file listing the Python packages that are required by
15+
the Python script in `src`.
16+
17+
18+
## How to create the applications?
19+
20+
1. Create a virtual environment and install the required packages:
21+
```bash
22+
$ python -m venv build_venv
23+
$ source build_venv/bin/activate
24+
(venv)$ pip install -r requirements.txt
25+
```
26+
1. Create the application:
27+
```bash
28+
(venv)$ pyinstaller \
29+
--onefile \
30+
--hidden-import funcs \
31+
--collect-submodules funcs \
32+
src/sum_columns.py
33+
(venv)$ pyinstaller \
34+
--onefile \
35+
--add-data data.csv:data.csv \
36+
--hidden-import funcs \
37+
--collect-submodules funcs \
38+
src/sum_columns.py
39+
```

source-code/pyinstaller/data.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A,B,C
2+
1.2,2.3,4.5
3+
2.1,3.2,5.4
4+
3.3,5.5,9.9
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyinstaller
2+
pandas
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import pathlib
5+
import sys
6+
from lib.funcs import prod_columns
7+
8+
def main():
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument('input_file', nargs='?',
11+
help='Input CSV file')
12+
parser.add_argument('--version', action='store_true',
13+
help='Print Python version and exit')
14+
args = parser.parse_args()
15+
if args.version:
16+
print(sys.version)
17+
sys.exit(0)
18+
if args.input_file is None:
19+
data_dir = pathlib.Path(__file__).parent
20+
prod_columns(data_dir / 'data.csv')
21+
else:
22+
prod_columns(args.input_file)
23+
24+
25+
if __name__ == '__main__':
26+
main()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import sys
5+
from lib.funcs import sum_columns
6+
7+
def main():
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('input_file', help='Input CSV file')
10+
parser.add_argument('--version', action='store_true',
11+
help='Print Python version and exit')
12+
args = parser.parse_args()
13+
if args.version:
14+
print(sys.version)
15+
sys.exit(0)
16+
sum_columns(args.input_file)
17+
18+
19+
if __name__ == '__main__':
20+
main()

0 commit comments

Comments
 (0)