Skip to content

Commit bbc3fa9

Browse files
committed
update readme
1 parent 7831443 commit bbc3fa9

3 files changed

Lines changed: 72 additions & 35 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
api.spec
2+
.venv/
23
node_modules/
34
build/
45
dist/

README.md

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This post shows how to use Electron as the GUI component of Python applications.
66

77
## important notice
88

9-
The following are copied from my [original post](https://www.fyears.org/2017/02/electron-as-gui-of-python-apps-updated.html). They should be the same.
9+
The following are copied from my [original post](https://www.fyears.org/2017/02/electron-as-gui-of-python-apps-updated.html). They should be the same. **If there are inconsistency, the `README.md` on the GitHub repo is more accurate.**
1010

1111
## original post and debates
1212

@@ -125,16 +125,13 @@ And the `index.html`, `main.js`, `package.json` and `renderer.js` are modified f
125125

126126
First of all, since we already have the Python application running, the Python environment should be fine. I strongly recommend developing Python applications in `virtualenv`.
127127

128-
Try install `zerorpc`, and `pyinstaller` (for packaging).
128+
Try install `zerorpc`, and `pyinstaller` (for packaging). On Linux / Ubuntu we may need to run `sudo apt-get install libzmq3-dev` **before** `pip install`.
129129

130130
```bash
131131
pip install zerorpc
132132
pip install pyinstaller
133-
```
134-
135-
If working on Windows, also
136133

137-
```bash
134+
# for windows only
138135
pip install pypiwin32 # for pyinstaller
139136
```
140137

@@ -154,7 +151,7 @@ We need to configure the `package.json`, especially the `main` entry:
154151
"start": "electron ."
155152
},
156153
"dependencies": {
157-
"zerorpc": "*"
154+
"zerorpc": "fyears/zerorpc-node"
158155
},
159156
"devDependencies": {
160157
"electron": "^1.4.1",
@@ -163,53 +160,83 @@ We need to configure the `package.json`, especially the `main` entry:
163160
}
164161
```
165162

166-
Ironically, to compile Node.js C/C++ native codes, we need to have `python2` configured, no matter what Python version we are using for our Python application. Check out the [official guide](https://github.com/nodejs/node-gyp).
163+
Clean the caches:
167164

168-
If working on Windows, open PowerShell **as Administrator**, and run
165+
```bash
166+
# On Linux / OS X
167+
# clean caches, very important!!!!!
168+
rm -rf ~/.node-gyp
169+
rm -rf ~/.electron-gyp
170+
rm -rf ./node_modules
171+
```
172+
173+
```powershell
174+
# On Window PowerShell (not cmd.exe!!!)
175+
# clean caches, very important!!!!!
176+
Remove-Item "$($env:USERPROFILE)\.node-gyp" -Force -Recurse -ErrorAction Ignore
177+
Remove-Item "$($env:USERPROFILE)\.electron-gyp" -Force -Recurse -ErrorAction Ignore
178+
Remove-Item .\node_modules -Force -Recurse -ErrorAction Ignore
179+
```
180+
181+
Then run `npm`:
169182

170183
```bash
171-
npm install --global --production windows-build-tools
184+
# 1.4.15 is the version of electron
185+
# It's very important to set the electron version correctly!!!
186+
# check out the version value in your package.json
187+
npm install --runtime=electron --target=1.4.15
188+
189+
# verify the electron binary and its version by opening it
190+
./node_modules/.bin/electron
172191
```
173192

174-
The above command installs Python 2.7 in `%USERPROFILE%\.windows-build-tools\python27` and other VS libraries. Everything should be set and should not cause any conflict with the previously-installed Python environment.
193+
The `npm install` will install `zerorpc-node` from [my fork](https://github.com/0rpc/zerorpc-node/pull/84) to skip building from sources.
175194

176-
Also download VC 2010 runtime from [here](https://www.microsoft.com/en-us/download/details.aspx?id=14632) due to a [bug](https://github.com/JustinTulloss/zeromq.node/issues/582).
195+
All libraries should be fine now.
177196

178-
Then set the `npm` [for Electron](https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md), and install the required libraries.
197+
#### optional: building from sources
179198

180-
On Linux / OS X:
199+
If the above installation causes any errors **even while setting the electron version correctly**, we may have to build the packages from sources.
200+
201+
Ironically, to compile Node.js C/C++ native codes, we need to have `python2` configured, no matter what Python version we are using for our Python application. Check out the [official guide](https://github.com/nodejs/node-gyp).
202+
203+
Especially, if working on Windows, open PowerShell **as Administrator**, and run `npm install --global --production windows-build-tools` to install a separated Python 2.7 in `%USERPROFILE%\.windows-build-tools\python27` and other required VS libraries. We only need to do it at once.
204+
205+
Then, **clean `~/.node-gyp` and `./node_modules` caches as described above at first.**
206+
207+
Set the `npm` [for Electron](https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md), and install the required libraries.
208+
209+
Set the environment variables for Linux (Ubuntu) / OS X / Windows:
181210

182211
```bash
212+
# On Linux / OS X:
213+
183214
# env
184215
export npm_config_target=1.4.15 # electron version
185-
export npm_config_arch=x64
186-
export npm_config_target_arch=x64
187-
export npm_config_disturl=https://atom.io/download/electron
188216
export npm_config_runtime=electron
217+
export npm_config_disturl=https://atom.io/download/electron
189218
export npm_config_build_from_source=true
190-
npm config ls
191219

192-
# clean caches, very important!!!!!
193-
rm -rf ~/.node-gyp
194-
rm -rf ~/.electron-gyp
195-
rm -rf ./node_modules
196-
```
220+
# may not be necessary
221+
#export npm_config_arch=x64
222+
#export npm_config_target_arch=x64
197223

198-
On Windows PowerShell, set environement variables in [different way](http://stackoverflow.com/questions/714877/setting-windows-powershell-path-variable):
224+
npm config ls
225+
```
199226

200227
```powershell
228+
# On Window PowerShell (not cmd.exe!!!)
229+
201230
$env:npm_config_target="1.4.15" # electron version
202-
$env:npm_config_arch="x64"
203-
$env:npm_config_target_arch="x64"
204-
$env:npm_config_disturl="https://atom.io/download/electron"
205231
$env:npm_config_runtime="electron"
232+
$env:npm_config_disturl="https://atom.io/download/electron"
206233
$env:npm_config_build_from_source="true"
207-
npm config ls
208234
209-
# clean caches, very important!!!!!
210-
Remove-Item "$($env:USERPROFILE)\.node-gyp" -Force -Recurse -ErrorAction Ignore
211-
Remove-Item "$($env:USERPROFILE)\.electron-gyp" -Force -Recurse -ErrorAction Ignore
212-
Remove-Item .\node_modules -Force -Recurse -ErrorAction Ignore
235+
# may not be necessary
236+
#$env:npm_config_arch="x64"
237+
#$env:npm_config_target_arch="x64"
238+
239+
npm config ls
213240
```
214241

215242
Then install things:
@@ -220,9 +247,10 @@ Then install things:
220247

221248
# install everything based on the package.json
222249
npm install
223-
```
224250

225-
All libraries should be fine now.
251+
# verify the electron binary and its version by opening it
252+
./node_modules/.bin/electron
253+
```
226254

227255
## core functions
228256

@@ -524,6 +552,14 @@ Copy / Move the folder(s) to anywhere or other machines to check the result! :-)
524552

525553
See [GitHub `electron-python-example`](https://github.com/fyears/electron-python-example).
526554

555+
### solutions to errors
556+
557+
[issue #6](https://github.com/fyears/electron-python-example/issues/6): `... failed with KeyError`
558+
559+
[issue #7](https://github.com/fyears/electron-python-example/issues/7): `Uncaught Error: Module version mismatch. Expected 50, got 48.`
560+
561+
Uninstall everything, **set up the npm environment variables correctly especially for the electron version**, remember to `activate` the virtualenv if using Python `virtualenv`.
562+
527563
### further optimization?
528564

529565
Trim some unnecessary files in Python executable by configuring `pyinstaller` further. Trim Electron (is it possible?). Use even faster IPC methods (though `ZeroMQ` is one of the fastest in most cases).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "fyears",
1717
"license": "MIT",
1818
"dependencies": {
19-
"zerorpc": "*"
19+
"zerorpc": "fyears/zerorpc-node"
2020
},
2121
"devDependencies": {
2222
"electron": "^1.4.1",

0 commit comments

Comments
 (0)