vscode-code-runner
vscode-code-runner copied to clipboard
pipenv venv not activated before Code Runner executes Python script file
Windows 10 1803 (17134.407) Python 3.7.1 pipenv 2018.10.13 VSCode 1.29.1 Code Runner 0.9.5
When running a Python script using Code Runner the virtualenv is not activated before the code is run in the terminal for the first time, causing my code fail. The virtualenv is activated after the script fails and I can then run my code successfully until I close the terminal window. A minimal working example is shown below.
The venv is activated in VSCode and the correct Python interpreter is configured.
Here is the Pipfile for the project:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
numpy = "*"
[dev-packages]
[requires]
python_version = "3.7"
And here are all the Code Runner settings from my settings.json file:
{
"code-runner.fileDirectoryAsCwd": true,
"code-runner.respectShebang": false,
"code-runner.ignoreSelection": true,
}
While I can get my code to work this is rather annoying. Does anyone have any ideas what might be causing this?
I have the same issue. (Using anaconda venviroments)
See : https://github.com/formulahendry/vscode-code-runner/issues/272
See : #272
I did see that when searching for a solution and I don't think it applies. Code Runner fails even when the proper environment has been set or when
"code-runner.executorMap": {
"python":"$pythonPath $fullFileName",
}
is set in settings.json
. The issue is that Code Runner executes the code being run before VSCode has the chance to activate the appropriate venv.
HI, have you set the right python envinronment using "Command Palette" --> Python: Select interpreter" ?
I have installed pyenv.
I did set the interpreter, however it doesnt activate my base environment untill after the first run of the code, which i find weird.
@cocoaway Yes, I have set the python environment manually. VSCode also detects the presence of the .venv file and activates the correct environment automatically as was the case in the gif I posted above. However, Code Runner fires off before the activation can happen; i.e., before .venv\Scripts\activate.ps1
gets called. You can also see this happening in my gif above.
I am having the same issue too. I use anaconda and code-runner run the python file with base python interpreter despite selecting the virtual env interpreter.
Same issue here, runner doesnt seem to update when using pipenv virtual env. got my env properly selected, i had to override the "run custom command" but it seems to be not ideal.
I have the same issue with conda-environments.
same issue with conda environments
note: works ok now - did not notice that I had a shebang path at the beginning of the file
I've been having the same issue. I've changed the executer map for python to :
"python": "[ -f 'Pipfile' ] python || pipenv run python",
It's checks if the Pipfile exists and runs it in the venv, otherwise it will jsut use the system python. I believe this will only work systems using Bash or something similar. Not sure what the windows counterpart would be.
Having the same issue here. I seemed to have fixed it by both making it use the integrated terminal (zsh) and setting the executor map like such:
"code-runner.executorMap": {
"python": "$pythonPath $fullFileName",
},
"code-runner.runInTerminal": true
Hope this can help someone.
I don't have anything new to add other than 5 months later I am seeing the same issue. The settings.json
file that is autogenerated has content similar to:
{
"python.pythonPath": "C:\\Users\\pconwell\\.virtualenvs\\ridgeplots-0xoO3BBx\\Scripts\\python.exe"
}
Which will not work at all. VS Code is (presumably) recognizing the virtual environment correctly as the code will run manually from terminal AND the correct virtual environment is showing the lower left corner of VS Code. If I use the above settings, I will get a 'module not found error'.
If I change the above code to:
{
"python.venvPath": "C:/Users/pconwell/.virtualenvs",
"python.pythonPath": "C:/Users/pconwell/.virtualenvs/ridgeplots-0xoO3BBx/Scripts/python.exe"
"code-runner.executorMap": {
"python": "$pythonPath $fullFileName",
}
"code-runner.runInTerminal": true
}
The Code Runner will work BUT only after I run it the first time. (i.e. the first time I run Code Runner it gives an error, but the second time I run Code Runner it works.) The specific error varies from project to project, but the error will inevitably magically disappear the second time Code Runner is run.
Can confirm this is still an issue with Visual Studio Code 1.37.1 and Code Runner 0.9.13. Seems like Code Runner runs before Visual Studio has the chance to setup env. Works fine in terminal providing you manually activate it pipenv shell
but not in output.
Following the advice above to change executor to
"code-runner.executorMap": {
"python": "$pythonPath $fullFileName",
},
completely fixed it for me. Windows 10
There should be another option code-runner.activateEnvironment
or similar that should wait for Visual Studio Code python.terminal.activateEnvironment
.
Ctrl+Alt+N doesn't appear to work with CodeRunner and Anaconda. Works on another computer with just the regular Python 3.6 distro installed.
Running VSCode 1.37.1 (user setup) with CodeRunner 0.9.14
Any idea on how to get this to work using the keyboard shortcut?
I tried the solutions above, working with flask, and it doesn't run at all. Actually, following @pconwell's settings above, I can right-click and run in terminal, but not use code runner.
A quick solution I found then was to save pconwell's settings in the workspace settings, and also to make a 'run in terminal' shortcut. Then I can use coderunner for my non-virtual environment projects and I can use the terminal shortcut when I'm messing with virtual environments.
Simplest solution to this bug is to just run the second suggested piece 👍 "code-runner.runInTerminal": true ,by adding this to the settings.json file in .vscode python code now runs in Terminal and is able to properly set up environment variables before running the code
@buckybots , can you specify exactly what you are saying? For example, here is my settings.json file which does not work correctly on first run:
{
"python.venvPath": "C:/Users/pconwell/.virtualenvs",
"python.pythonPath": "C:/Users/pconwell/.virtualenvs/project-6oXFOUjc/Scripts/python.exe"
"code-runner.executorMap": {
"python": "$pythonPath $fullFileName",
}
"code-runner.runInTerminal": true
}
I am not using a venv, but I have the same issue with my environment variables. I see you did add the "code-runner.runInTerminal" :true statement which fixed the issue for me. It might be that you are missing some commas between entries. Try adding after " .... .exe", and also after "....$fullFileName",} ,
On Mon, Oct 21, 2019 at 12:50 PM Patrick Conwell [email protected] wrote:
@buckybots https://github.com/buckybots , can you specify exactly what you are saying? For example, here is my settings.json file which does not work correctly on first run:
{ "python.venvPath": "C:/Users/pconwell/.virtualenvs", "python.pythonPath": "C:/Users/pconwell/.virtualenvs/project-6oXFOUjc/Scripts/python.exe"
"code-runner.executorMap": { "python": "$pythonPath $fullFileName", } "code-runner.runInTerminal": true
}
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/formulahendry/vscode-code-runner/issues/395?email_source=notifications&email_token=ABCDDG7Y4PC45DREKB6LOWTQPXMV5A5CNFSM4GF2OQNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEB3AK5Q#issuecomment-544605558, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCDDG77GPS4SVFATVQFTODQPXMV5ANCNFSM4GF2OQNA .
@buckybots , thanks for the suggestion, but adding the commas does not fix the issue.
I am not using a venv
Not to be rude, but if you are not using venv and this issue is related to venv... Your solution - while appreciated - is neither related to nor a solution for the issue being discussed.
Actually, I would say you are being rude by stating that you are not being rude, but at any rate, I responded, because I am pretty sure I was responding to the issue, its about the environment variables, not really about them being set in a venv or not. I used this thread to identify and fix my issue. That being that the environment variables were not being set. I am pretty sure this is true weather I was using a venv or not. I had environment variables, and their values were not set as described in the thread, and by using the solution of the thread it fixed my issue. I was just trying to help anyone else that had the same problem such as yourself. I am sorry that my suggestion of your missing commas did not help you out.
On Wed, Oct 23, 2019 at 7:37 AM Patrick Conwell [email protected] wrote:
@buckybots https://github.com/buckybots , thanks for the suggestion, but adding the commas does not fix the issue.
I am not using a venv
Not to be rude, but if you are not using venv and this issue is related to venv... Your solution - while appreciated - is neither related to nor a solution for the issue being discussed.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/formulahendry/vscode-code-runner/issues/395?email_source=notifications&email_token=ABCDDGYLOJHYABCYLBXH4TTQQAZRHA5CNFSM4GF2OQNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECBCYKI#issuecomment-545401897, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCDDG7V4XFD4NBCH4ZJNVLQQAZRHANCNFSM4GF2OQNA .
After some experimenting, I found a simple (if ugly) "solution" (definitely not a real solution) that seems to be working for me on Windows. It seems that code runner initially runs the file with a keyboard input waiting to be read (i.e. msvcrt.kbhit()
is set to true) and it seems to be trying to write `source C:/Users/user/.virtualenvs/pipenv/Scripts/activate' to terminal but that text is being piped AFTER the file starts execution.
Running the file in any other way (as far as I can tell) starts with msvcrt.kbhit()
set to false, meaning that the terminal is not waiting for keyboard input.
Therefore, an ugly "solution" is to check if msvcrt.kbhit()
is true or false before continuing to execute the file. If the terminal is waiting for an input, we can 'trick' it with input("")
which will close out the open keyboard read.
Putting this at the very top of my file seems to work around the issue (on Windows):
# workaround for code runner
import msvcrt
if msvcrt.kbhit():
input("")
I'm not really sure why this works, to be honest. I'm really just guessing here, but I'm wondering if code runner IS executing the activation script but is then keeping the text in buffer and it's getting piped to the terminal twice on accident?
Also, just to clarify, here is my settings.json file:
{
"python.venvPath": "C:/Users/user/.virtualenvs",
"python.pythonPath": "C:/Users/user/.virtualenvs/pipenv/Scripts/python.exe",
"code-runner.executorMap": {"python": "$pythonPath -u $fullFileName",},
"code-runner.runInTerminal": true
}
Same issue on mac and using venv. Venv works well but code runner doesn't choose the correct interpreter after modifying the configuration.
Having the same issue here. I seemed to have fixed it by both making it use the integrated terminal (zsh) and setting the executor map like such:
"code-runner.executorMap": { "python": "$pythonPath $fullFileName", }, "code-runner.runInTerminal": true
Hope this can help someone.
This fixes the errors, but its just a work around.
With this fix the first time I run code runner after opening VS Code:
- New terminal window is opened
- Python runs from selected environment
- Activates the environment
That approach kind off doesn't need the third step
Did anybody find a little bit more elegant way of forcing "(3.) activation of environment" before "(2.) first execution"?
Thanks!
Below i submit how it looks in terminal.
@YannickGibson am having the same issue! have you managed to get a solution for it?
@AmericanY No, I'm still using the compromise method. I am looking forward to seeing somebody that has found a better solution.
@YannickGibson am using the following way:
"code-runner.executorMap": {
"python": "~\\Desktop\\Lab\\env\\Scripts\\Activate.ps1 && $pythonPath -u $fullFileName"
}
@AmericanY I don't actually think this is a good solution. It just forces the activation of environment everytime you run the script, plus the environment in your executor map is fixed. So every time you want to use different environment you got to change the executor map. If you press CTRL+ SHIFT + P => "Select Interpreter" => Select your environment. Then just "$pythonPath -u $fullFileName" will automatically run the selected environment for you.
I'm sure this is terrible because I don't know what I am doing, but I have a "solution".
"python.terminal.activateEnvironment": false,
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"python": "pipenv run python $fileName",
},
This works with activateEnvironment set to true, but after the first run it will give you a warning about how you're trying to run an environment inside an environment.
I was facing the same problem. What worked for me was to activate the environment before launching vscode.
E.g. if you're working with venv and have in your python project folder a venv named ".env"
- open a terminal and cd into your python project directory
- activate your python environment with
source .env/bin/activate
- start vscode with
code .
- launch python file with Code Runner.