summaryrefslogtreecommitdiffstats
path: root/md/writeup/compile_python.md
blob: 2acbb1e0463b6628892c75df361596d0dbfc3b84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
title:Compile static python
keywords:linux,python,standalone

# Compile static python

## Intro

Not all distros or environment have decent version of python. Even of year when
python2 is deprecated some cases when system are stuck to some particular 
version of python just becouse of there is no new updated for distribuations.
Only options is to compile lates python by yourself and stick to latest version
of python. To easy distribution there is preference of static compilation. 
Also need of admin priviledges not needed if you compile and use recent staticly
compiled python version. One of the example Centos6 its still used, but nothing
is updated for this distro ... and if you whant to use freshest packages without
admin permissions then its probably one way to go.

## Python 2


### Download

```
wget -c https://www.python.org/ftp/python/$(VERSION)/Python-$(VERSION).tgz
tar -xvf Python-$(VERSION).tgz
```

### Configure

Set flags to make python compiled as static

```
./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-fPIC -static" --disable-shared --prefix=/custom/install/path
```

Inside Python directory there is files to change.
Change Modules/Setup

add 
*static*
and uncomment modules that would like to have builtin. It make some time to 
try and run if any of modules have issuse.

Here example file used  
[http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup)

### Compile

```
make
```

### Install

Instalaton is going copy static python and modules that arent compiled to 
configured installed path. 



```
make install
```

Later on set env variables to point to correct location of custom installed python
so can import all modules from correct location

```
PYTHONPATH=/custom/output/lib
PYTHONHOME=/custom/output
```


## Python 3

### Download
### Configure
Set flags to make python compiled as static

```
./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-fPIC -static" --disable-shared --prefix=/custom/install/path
```

Inside Python directory there is files to change.
Change Modules/Setup

add 
*static*
and uncomment modules that would like to have builtin. It make some time to 
try and run if any of modules have issuse.

Here example file used  
[http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup)

### Compile
Same as python2

### Install
Same as python2

## Testing

### ArchLinux
Did work by default without any changes

### Raspi4

Disabled this modules to make it compile

```
_socket
_posix
pwd
dl
```

Got alot of linking warnings but did worked and installed at the end.

Selftest failed on:
```
0:01:10 load avg: 0.85 [119/404/14] test_email
make: *** [Makefile:884: test] Segmentation fault
```

### Centos6
Needed to disable modules in Modules/Setup to make it compile
```
_socket
_posix
pwd
dl
```

Gcc suggested to use this options
```
-pie -fPIC
```

Failed last state complaining that same version compiled libs should be used.
Local setup issue probably.

## Compilation snippet

Here is located compilation snippet that used to test static compilation

[http://git.main.lv/cgit.cgi/compilation-snippets.git/](http://git.main.lv/cgit.cgi/compilation-snippets.git/)

Clone it, goto python directory

#### Python2
```
make download
cp py2/Setup Python-2.X.X/Modules/Setup
make configure
make compile
make install
```

Output is in **output** directory

#### Python3
```
make download3
cp py3/Setup Python-3.X.X/Modules/Setup
make configure3
make compile3
make install3
```

Output is in __output3__ directory

## Links

[https://wiki.python.org/moin/BuildStatically](https://wiki.python.org/moin/BuildStatically)  
[https://gist.github.com/ajdavis/9632280](https://gist.github.com/ajdavis/9632280)  
[https://stackoverflow.com/questions/1150373/compile-the-python-interpreter-statically](https://stackoverflow.com/questions/1150373/compile-the-python-interpreter-statically)  
[http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py2/Setup)  
[http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup](http://git.main.lv/cgit.cgi/compilation-snippets.git/tree/python/py3/Setup)