Skip to content

Commit 70f0951

Browse files
committed
working PYTHONPATH
1 parent f409b8d commit 70f0951

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

build/install-spinner.gif

-638 KB
Loading

src/browser/services/env.js

+45-24
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@ import path from 'path';
88
const rootAppDir = process.resourcesPath,
99
condaDirName = 'conda',
1010
condaDir = path.join(rootAppDir, condaDirName),
11+
dllDir = path.join(rootAppDir, condaDirName, 'DLLs'),
1112
libDir = path.join(rootAppDir, condaDirName, 'Lib'),
13+
sitePackagesDir = path.join(rootAppDir, condaDirName, 'Lib', 'site-packages'),
1214
scriptsDir = path.join(rootAppDir, condaDirName, 'Scripts');
1315

1416
console.log({rootAppDir, condaDirName, condaDir, libDir, scriptsDir, dirname: __dirname});
1517

18+
function splitList(list) {
19+
if (process.platform === 'win32') {
20+
return list.split(';');
21+
} else {
22+
return list.split(':');
23+
}
24+
}
25+
26+
function joinList(list) {
27+
if (process.platform === 'win32') {
28+
return list.join(';');
29+
} else {
30+
return list.join(':');
31+
}
32+
}
33+
1634
function prependToPath(fullPath, pathPart) {
1735
if (!_.includes(fullPath, pathPart)) {
1836
fullPath.unshift(pathPart);
@@ -42,31 +60,43 @@ function addBonusVariables(env) {
4260
function prependBuiltinPath(env) {
4361
const myPath = getPath(env);
4462

45-
prependToPath(myPath, condaDir);
46-
prependToPath(myPath, libDir);
4763
prependToPath(myPath, scriptsDir);
64+
prependToPath(myPath, libDir);
65+
prependToPath(myPath, condaDir);
4866

4967
return setPath(env, myPath);
5068
}
5169

70+
function addOurPythonPath(env) {
71+
if (process.platform === 'win32') {
72+
const list = env.PYTHONPATH ? splitList(env.PYTHONPATH) : [];
73+
74+
prependToPath(list, sitePackagesDir);
75+
prependToPath(list, libDir);
76+
prependToPath(list, dllDir);
77+
78+
env.PYTHONPATH = joinList([dllDir, libDir, sitePackagesDir], list);
79+
}
80+
81+
return env;
82+
}
83+
5284
function applyBuiltinPython(env) {
5385
const useBuiltinPython = local.get('useBuiltinPython') || 'failover',
54-
hasPythonFailedOver = session.get('hasPythonFailedOver') || false;
86+
hasPythonFailedOver = session.get('hasPythonFailedOver') || false,
87+
hasPythonPath = env.PYTHONPATH;
5588

56-
if (useBuiltinPython === 'yes' || (hasPythonFailedOver && useBuiltinPython === 'failover')) {
89+
if (!hasPythonPath || useBuiltinPython === 'yes' || (hasPythonFailedOver && useBuiltinPython === 'failover')) {
90+
addOurPythonPath(env);
5791
env = prependBuiltinPath(env);
5892
}
5993

6094
return env;
6195
}
6296

6397
function getEnvironmentVariables(env) {
64-
65-
console.log('what, HUH?', env);
66-
6798
if (!env) {
6899
env = local.get('environmentVariables');
69-
console.log('wait, WHY?', env);
70100
}
71101

72102
if (env) {
@@ -94,11 +124,7 @@ function getPath(env) {
94124
let result;
95125

96126
if (path) {
97-
if (process.platform === 'win32') {
98-
result = path.split(';');
99-
} else {
100-
result = path.split(':');
101-
}
127+
result = splitList(path);
102128
} else {
103129
result = [];
104130
}
@@ -108,28 +134,23 @@ function getPath(env) {
108134

109135
function setExistingPath(env, value) {
110136
if (env.PATH) {
111-
env.PATH = value;
137+
delete value.PATH;
112138
} else if (env.Path) {
113-
env.Path = value;
139+
delete value.Path;
114140
} else if (env.path) {
115-
env.path = value;
116-
} else {
117-
// most OS's use all capitals, and if this is not set, the user has done something WEIRD
118-
env.PATH = value;
141+
delete env.path;
119142
}
120143

144+
env.PATH = value;
145+
121146
return env;
122147
}
123148

124149
function setPath(env, newPath) {
125150
let result;
126151

127152
if (_.isArray(newPath)) {
128-
if (process.platform === 'win32') {
129-
result = newPath.join(';');
130-
} else {
131-
result = newPath.join(':');
132-
}
153+
result = joinList(newPath);
133154

134155
env = setExistingPath(env, result);
135156
local.set('environmentVariables', env);

src/node/kernels/python/client.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ function createObjectEmitter(stream) {
7171
return emitter;
7272
}
7373

74+
function isMissingRodeoDependency(client, data) {
75+
const match = data.match(/Exception: (.+) is not installed/);
76+
77+
if (match && match[1]) {
78+
const error = new Error(match[1] + ' is not installed');
79+
80+
error.missingPackage = match[1];
81+
82+
client.emit('error', error);
83+
}
84+
}
85+
86+
function isMissingImport(client, data) {
87+
const match = data.match(/ImportError: No module named '(.+)'/);
88+
89+
if (match && match[1]) {
90+
const error = new Error(match[1] + ' is not installed');
91+
92+
error.missingPackage = match[1];
93+
94+
client.emit('error', error);
95+
}
96+
}
97+
7498
/**
7599
* @param {JupyterClient} client
76100
* @param {string} source
@@ -79,15 +103,8 @@ function createObjectEmitter(stream) {
79103
function handleProcessStreamEvent(client, source, data) {
80104
if (source === 'stderr.data') {
81105
data = data.toString();
82-
const match = data.match(/Exception: (.+) is not installed/);
83-
84-
if (match && match[1]) {
85-
const error = new Error(match[1] + ' is not installed');
86-
87-
error.missingPackage = match[1];
88-
89-
client.emit('error', error);
90-
}
106+
isMissingRodeoDependency(client, data);
107+
isMissingImport(client, data);
91108
}
92109

93110
client.emit('event', source, data);

0 commit comments

Comments
 (0)