Open tatecapone opened 5 years ago
i have a same problem..
have you installed python 3.7?
yes, I have installed python 3 and the path indicated in the settings of vs code
I just installed this extension and I have the same problem. On a Mac, with python 2.7 installed by default, I've installed python 3.6 alongside it, and used Python: Select Interpreter command in VS Code command pallet to set my interpreter path to python 3.6. Nevertheless, when I open a python preview it says that it is using Python 2.7.10.
For me, my default python is in /usr/bin/python
:
$ which python
/usr/bin/python
$ python --version
Python 2.7.10
Beacuase in my environment path, /usr/local/bin
is ahead of /usr/bin
which means it will search /usr/local/bin
first, so I create a link to python3 in /usr/local/bin
ln -s "$(which python3)" /usr/local/bin/python
I had a similar problem.
But I use Anaconda (miniconda). While Anaconda was higher in my path than other installs of Python,
$ echo $PATH
/home/mike/miniconda3/bin:/home/mike/miniconda3/condabin:/usr/local/bin:/home/mike/.cargo/bin:/home/mike/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
it was not being recognized by VS Code Preview. It was, however, being recognized as my Python version from the CLI (as well as my version of Python used by VS Code outside of Preview):
$ which python
/home/mike/miniconda3/bin/python
$ python --version
Python 3.7.1
I do have another version, a 2.7 version, living in /usr/bin/python
:
$ ls -altr /usr/bin/python
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7*
$ which python2.7
/usr/bin/python2.7
$ python2.7 --version
Python 2.7.15+
I used the advice given by @Nero5023 above, ensuring that /usr/local/bin
pointed towards Python 3 and that it was ahead of /usr/bin
in my $PATH
. This solved the problem.
I wanted to mention this here, however, because to me this seems like a bug: if $PATH
priority is how the Python version is chosen, why was the Anaconda version ignored, which was the highest priority version on my path?
Is there a reason the extension couldn't just use whatever interpreter is specified via the python.pythonPath
setting?
I verified @pzelnip 's point: I checked in my User
and Workspace
settings, and in fact no matter what is in those settings, the $PATH
is used instead. (Sorry if that was already obvious to others, it was not to me.)
I agree it would make more sense to use those settings, since this is how Visual Studio Code works in all other regards. It would be more consistent with standard practice.
(BTW, this still does not explain why the $PATH
itself is not followed properly. As I stated above, the miniconda
portion of my $PATH
is ignored, while the rest is properly read in. This is my base miniconda, meaning that I am not activating any miniconda environment. Moreover, other Python extensions are able to access the miniconda version of Python.)
I am also saddened that it doesnt follow the $PATH variable, on linux python defaults to python2 and python3 needs to be explicitly called to run python3
My friend tried to rebuild the extension from source to try and fix the issue with no luck, however I found the solution, its a bit hacky but it works. I looked at the source for the mention of args.pythonpath and found that it was referenced in 2 places,
line 68 in: .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients/localDebugClient.js line 54 in: .vscode/extensions/dongli.python-preview-0.0.4/out/features/previewManager.js
(edited for clarity)
The folder where its stored (on linux) is the .vscode folder in the home directory (~/.vscode). I navigated there and to the extensions directory, then to the python-preview directory and simply changed python to python3 in those two spots and it now appears to be using python3. I hope this helps anyone who wants to use this extension with python3
@SugarBooty
After I went to the directory of the Python Preview extension using this command
cd .vscode/extensions/dongli.python-preview-0.0.4/
and I got to a few files and directories.
Among these there was only 1 pythonFiles
directory.
I renamed this directory to python3Files
but reverted back as the extension was not working.
The extension is still using Python 2.7 instead of Python 3.6
Could you tell me where I went wrong?
Where did you change python to python3 ?
@Karan-U-Kanthawar Hello, you werent supposed to rename the directory. Going through the steps to help you I noticed that the line numbers I gave are incorrect as well, and I have updated them.
Please revert any changes you have made to the file, or uninstall and reinstall the extension before following the following steps
I am assuming that you are running linux, if not please let me know and I will redo the instructions
Close VScode
Go into the first directory I mentioned in my post:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients
Next, open the localDebugClient.js file using nano:
nano localDebugClient.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 68 and hit enter.
You will see that your cursor has been moved to a line that says:
let pythonPath = 'python';
Change 'python'
to 'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.
Next, do it to the other file mentioned:
Open the second directory I mention:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/features
Open the previewManager.js file using nano:
nano previewManager.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 54 and hit enter.
You will see that your cursor has been moved to a line that says:
pythonPath: 'python'
Change 'python'
to 'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.
Now when you launch the extension it should show that its using python3
If you have any issues, please let me know and I will try to help. This extension was not made for python3 and might not work as intended for it, and I did not write it nor could I get it to compile correctly when I downloaded the source
Best of luck!
Yes, it works on Python 3 now. Thanks alot! @SugarBooty
But it not works whith a virtualenv path.. I try ".\venv\Scripts\python.exe" and many other combinstion. But work only "d:\progekt_path\name_projekt\venv\scripts\python.exe" This is not interesting change the path pemanently..
A simple workaround on Ubuntu:
sudo apt install python-is-python3
python-is-python3 ships a symlink to point the /usr/bin/python interpreter at the current default python3
My friend tried to rebuild the extension from source to try and fix the issue with no luck, however I found the solution, its a bit hacky but it works. I looked at the source for the mention of args.pythonpath and found that it was referenced in 2 places,
line 68 in: .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients/localDebugClient.js line 54 in: .vscode/extensions/dongli.python-preview-0.0.4/out/features/previewManager.js
(edited for clarity)
The folder where its stored (on linux) is the .vscode folder in the home directory (~/.vscode). I navigated there and to the extensions directory, then to the python-preview directory and simply changed python to python3 in those two spots and it now appears to be using python3. I hope this helps anyone who wants to use this extension with python3
Thanks. It is working now with my python3 version.
anyone know how to have it work even with Anaconda or virtual env on windows? Im new to programming so a lot of it is a little over my head, especially when trying to translate between $PATH variable in linux vs windows. @SugarBooty even after trying your method, it still doesn't work for me so maybe its because my only interpreter is the anaconda version of python?
Good day. Found solution: in folder .vscode/extensions/dongli.python-preview-0.0.4/out/features/previewManager.js found two entries “pythonPath” and change them from python to python3. Worked for me, but maybe I missed something. Luck
I found a way to get the current python executable recognized by adopting the approach used by the great arepl plugin
navigate to your extension folder ~/.vscode\extensions\dongli.python-preview-0.0.4
install python-shell (don't just run npm i, because the extension is very old and it will complain about outdated vscode dependency)
npm i python-shell
save the patch below to pypath.patch and apply it using git apply < pypath.patch
diff --git a/out/debugger/common/areplUtilities.js b/out/debugger/common/areplUtilities.js
new file mode 100644
index 0000000..017a985
--- /dev/null
+++ b/out/debugger/common/areplUtilities.js
@@ -0,0 +1,29 @@
+'use strict';
+Object.defineProperty(exports, '__esModule', { value: true });
+const python_shell_1 = require('python-shell');
+const vscodeUtilities_1 = require('./vscodeUtilities');
+/**
+ * utilities specific to AREPL
+ */
+class areplUtils {
+ static getEnvFilePath() {
+ let envFilePath =
+ vscodeUtilities_1.default.getSettingOrOtherExtSettingAsDefault(
+ 'python',
+ 'envFile'
+ );
+ if (!envFilePath) envFilePath = '${workspaceFolder}/.env';
+ return vscodeUtilities_1.default.expandPathSetting(envFilePath);
+ }
+ static getPythonPath() {
+ let pythonPath =
+ vscodeUtilities_1.default.getSettingOrOtherExtSettingAsDefault(
+ 'python',
+ 'pythonPath'
+ );
+ if (!pythonPath) pythonPath = python_shell_1.PythonShell.defaultPythonPath;
+ return vscodeUtilities_1.default.expandPathSetting(pythonPath);
+ }
+}
+exports.default = areplUtils;
+//# sourceMappingURL=areplUtilities.js.map
diff --git a/out/debugger/common/vscodeUtilities.js b/out/debugger/common/vscodeUtilities.js
new file mode 100644
index 0000000..31145eb
--- /dev/null
+++ b/out/debugger/common/vscodeUtilities.js
@@ -0,0 +1,114 @@
+"use strict";
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+ return new (P || (P = Promise))(function (resolve, reject) {
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
+ });
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const vscode = require("vscode");
+const path_1 = require("path");
+class vscodeUtils {
+ /**
+ * expands ${} macros like ${workspaceFolder} or ${env:foo}
+ */
+ static expandSettingMacros(setting) {
+ setting = setting.replace("${workspaceFolder}", vscodeUtils.getCurrentWorkspaceFolder());
+ const envVar = setting.match(/\${env:([^}]+)}/);
+ if (envVar) {
+ setting = setting.replace("${env:" + envVar[1] + "}", process.env[envVar[1]]);
+ }
+ return setting;
+ }
+ /**
+ * returns absolute path with macros expanded
+ */
+ static expandPathSetting(path) {
+ path = this.expandSettingMacros(path);
+ // if its a relative path, make it absolute
+ if (path.includes(path_1.sep) && !path_1.isAbsolute(path)) {
+ path = vscodeUtils.getCurrentWorkspaceFolder() + path_1.sep + path;
+ }
+ return path;
+ }
+ /**
+ * returns doc eol as string;
+ * necessary because vscode stores it as a number for some stupid reason
+ */
+ static eol(doc) {
+ return doc.eol == 1 ? "\n" : "\r\n";
+ }
+ static newUnsavedPythonDoc(content = "") {
+ return __awaiter(this, void 0, void 0, function* () {
+ const pyDoc = yield vscode.workspace.openTextDocument({
+ content,
+ language: "python",
+ });
+ return vscode.window.showTextDocument(pyDoc);
+ });
+ }
+ /**
+ * returns block of text at lineNum, where a block is defined as a series of adjacent non-empty lines
+ */
+ static getBlockOfText(editor, lineNum) {
+ let block = editor.document.lineAt(lineNum).range;
+ while (block.start.line > 0) {
+ const aboveLine = editor.document.lineAt(block.start.line - 1);
+ if (aboveLine.isEmptyOrWhitespace)
+ break;
+ else
+ block = new vscode.Range(aboveLine.range.start, block.end);
+ }
+ while (block.end.line < editor.document.lineCount - 1) {
+ const belowLine = editor.document.lineAt(block.end.line + 1);
+ if (belowLine.isEmptyOrWhitespace)
+ break;
+ else
+ block = new vscode.Range(block.start, belowLine.range.end);
+ }
+ return block;
+ }
+ /**
+ * gets first highlighted text of active doc
+ * if no highlight or no active editor returns empty string
+ */
+ static getHighlightedText() {
+ const editor = vscode.window.activeTextEditor;
+ if (!editor)
+ return "";
+ return editor.document.getText(editor.selection);
+ }
+ /**
+ * returns current workspace folder uri or undefined if no folder open
+ */
+ static getCurrentWorkspaceFolderUri() {
+ const workspaceFolders = vscode.workspace.workspaceFolders;
+ if (workspaceFolders && workspaceFolders.length > 0 && workspaceFolders[0]) {
+ return workspaceFolders[0].uri;
+ }
+ }
+ /**
+ * returns current folder path.
+ * If no folder is open either returns friendly warning or empty string
+ */
+ static getCurrentWorkspaceFolder(friendlyWarning = true) {
+ const path = this.getCurrentWorkspaceFolderUri();
+ if (path)
+ return path.fsPath;
+ else
+ return friendlyWarning ? "could not find workspace folder" : "";
+ }
+ /**
+ * gets setting or if undefined/empty a setting from different extension.
+ * Assumes setting from other extension has the same name and type.
+ */
+ static getSettingOrOtherExtSettingAsDefault(otherExt, setting) {
+ const otherExtensionSettings = vscode.workspace.getConfiguration(otherExt, this.getCurrentWorkspaceFolderUri());
+ return otherExtensionSettings.get(setting);
+ }
+}
+exports.default = vscodeUtils;
+//# sourceMappingURL=vscodeUtilities.js.map
\ No newline at end of file
diff --git a/out/debugger/debugClients/localDebugClient.js b/out/debugger/debugClients/localDebugClient.js
index 4b459a9..a24a5b3 100644
--- a/out/debugger/debugClients/localDebugClient.js
+++ b/out/debugger/debugClients/localDebugClient.js
@@ -120,17 +120,19 @@ class LocalDebugClient extends baseDebugClient_1.BaseDebugClient {
}
handleProcessOutput(proc, failedToLaunch) {
proc.on('error', (error) => {
- const status = this.debugServerStatus;
- if (status === DebugServerStatus.Running) {
- return;
- }
- if (
- status === DebugServerStatus.NotRunning &&
- typeof error === 'object' &&
- error !== null
- ) {
- return failedToLaunch(error);
- }
+ try {
+ const status = this.debugServerStatus;
+ if (status === DebugServerStatus.Running) {
+ return;
+ }
+ if (
+ status === DebugServerStatus.NotRunning &&
+ typeof error === 'object' &&
+ error !== null
+ ) {
+ return failedToLaunch(error);
+ }
+ } catch (e) {}
// This could happen when the debugger didn't launch at all, e.g. python doesn't exist.
this.displayError(error);
this._previewManager.dispose();
diff --git a/out/features/previewManager.js b/out/features/previewManager.js
index d11b56f..f12b202 100644
--- a/out/features/previewManager.js
+++ b/out/features/previewManager.js
@@ -34,6 +34,7 @@ const previewConfig_1 = require('./previewConfig');
const preview_1 = require('./preview');
const dispose_1 = require('../common/dispose');
const pythonProcess_1 = require('../debugger/pythonProcess');
+const areplUtilities_1 = require('../debugger/common/areplUtilities');
const utils_1 = require('../debugger/common/utils');
const launcherProvider_1 = require('../debugger/debugClients/launcherProvider');
const localDebugClient_1 = require('../debugger/debugClients/localDebugClient');
@@ -82,6 +83,7 @@ class PythonPreviewManager {
this._debuggerLoaded = new Promise((resolve) => {
this._debuggerLoadedPromiseResolve = resolve;
});
+ const pythonPath = areplUtilities_1.default.getPythonPath();
this.createDebugger(this._launchArgs);
}
// 这段代码永远找不到已存在的preview,原因是preview的列输入参数是vscode.ViewColumn.Beside或者vscode.ViewColumn.Beside!!!
@@ -202,6 +204,7 @@ class PythonPreviewManager {
args.pythonPath = utils_1.getPythonExecutable(args.pythonPath);
} catch (ex) {}
this._launchArgs = args;
+ console.log('CreateDebugger launchArgs', args);
let launchScriptProvider =
new launcherProvider_1.DebuggerLauncherScriptProvider();
this._debugClient = new localDebugClient_1.LocalDebugClient(
@@ -260,6 +263,7 @@ class PythonPreviewManager {
if (!output) {
this._cachedOutputs.set(fileName, new pythonOutput_1.PythonOutput());
this.sendMessage(fileName, code);
+ output = this._cachedOutputs.get(fileName);
output.status = pythonOutput_1.PythonOutputStatus.Initialized;
} else {
// 如果是之后的传送,则设置定时器
I found a way to get the current python executable recognized by adopting the approach used by the great arepl plugin
- navigate to your extension folder ~/.vscode\extensions\dongli.python-preview-0.0.4
- install python-shell (don't just run npm i, because the extension is very old and it will complain about outdated vscode dependency)
npm i python-shell
- save the patch below to pypath.patch and apply it using
git apply < pypath.patch
this sounds like a great solutions, thanks!
But for me the diff patch fails as it looks like my "a" file has been reformatted and no longer matches, even though it says 0.0.4
I manually added the patch but i'm still not getting my vscode venv. This line here doesn't seem to go anywhere to me - but i'm not a JS programmer. Do we need to load that path somewhere?
const pythonPath = areplUtilities_1.default.getPythonPath();
Is there any chance of getting this extension to officially support the vscode env? It looks very cool extension but i've spent all morning trying to get it working without success :(
@Karan-U-Kanthawar Hello, you werent supposed to rename the directory. Going through the steps to help you I noticed that the line numbers I gave are incorrect as well, and I have updated them.
Please revert any changes you have made to the file, or uninstall and reinstall the extension before following the following steps
I am assuming that you are running linux, if not please let me know and I will redo the instructions
Close VScode Go into the first directory I mentioned in my post:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients
Next, open the localDebugClient.js file using nano:nano localDebugClient.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 68 and hit enter. You will see that your cursor has been moved to a line that says:let pythonPath = 'python';
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Next, do it to the other file mentioned: Open the second directory I mention:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/features
Open the previewManager.js file using nano:nano previewManager.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 54 and hit enter. You will see that your cursor has been moved to a line that says:pythonPath: 'python'
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Now when you launch the extension it should show that its using python3
If you have any issues, please let me know and I will try to help. This extension was not made for python3 and might not work as intended for it, and I did not write it nor could I get it to compile correctly when I downloaded the source
Best of luck!
Fix working on MacOS Monterey 12.0.1
and Visual Studio Code 1.62.3
@Karan-U-Kanthawar Hello, you werent supposed to rename the directory. Going through the steps to help you I noticed that the line numbers I gave are incorrect as well, and I have updated them.
Please revert any changes you have made to the file, or uninstall and reinstall the extension before following the following steps
I am assuming that you are running linux, if not please let me know and I will redo the instructions
Close VScode Go into the first directory I mentioned in my post:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients
Next, open the localDebugClient.js file using nano:nano localDebugClient.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 68 and hit enter. You will see that your cursor has been moved to a line that says:let pythonPath = 'python';
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Next, do it to the other file mentioned: Open the second directory I mention:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/features
Open the previewManager.js file using nano:nano previewManager.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 54 and hit enter. You will see that your cursor has been moved to a line that says:pythonPath: 'python'
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Now when you launch the extension it should show that its using python3
If you have any issues, please let me know and I will try to help. This extension was not made for python3 and might not work as intended for it, and I did not write it nor could I get it to compile correctly when I downloaded the source
Best of luck!
Confirming that issue still prevalent on version 0.0.4 . After changing the mentioned files on MacOs Monterey 12.2 and running VSCode 1.63.2, the issue is resolved thanks to this fix
Confirm that all the time, solution wich wrote @Karan-U-Kanthawar is current! Thank You Man ! Be careful, because in second directory you have two position with pythonPath: 'python' which you will have to change to 'python3'.
@Karan-U-Kanthawar Hello, you werent supposed to rename the directory. Going through the steps to help you I noticed that the line numbers I gave are incorrect as well, and I have updated them.
Please revert any changes you have made to the file, or uninstall and reinstall the extension before following the following steps
I am assuming that you are running linux, if not please let me know and I will redo the instructions
Close VScode Go into the first directory I mentioned in my post:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/debugger/debugClients
Next, open the localDebugClient.js file using nano:nano localDebugClient.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 68 and hit enter. You will see that your cursor has been moved to a line that says:let pythonPath = 'python';
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Next, do it to the other file mentioned: Open the second directory I mention:
cd .vscode/extensions/dongli.python-preview-0.0.4/out/features
Open the previewManager.js file using nano:nano previewManager.js
Then do the key combination ctrl + _ (ctrl + shift + the minus key) to bring up "Go to line". Type 54 and hit enter. You will see that your cursor has been moved to a line that says:pythonPath: 'python'
Change'python'
to'python3'
Do the key combination ctrl + s to save, and ctrl + x to exit.Now when you launch the extension it should show that its using python3
If you have any issues, please let me know and I will try to help. This extension was not made for python3 and might not work as intended for it, and I did not write it nor could I get it to compile correctly when I downloaded the source
Best of luck!
can you please write the instructions for Mac
Python preview uses Python 2.7 instead of Python 3.7, which is the one I have installed. How could I change this? Thank you!