VS Code Code Runner doesn't work with virtualenvs

丶灬走出姿态 提交于 2021-02-05 18:48:07

问题


I can't get Code Runner extension to work with virtualenvs. Whenever I try to run code that imports a library that is installed only in the virtualenv and not in the global Python installation I get an import error. Running the exact same code in terminal works.

I am on Windows 10 and I have Python 3.6.5 installed.

The precise error I am getting is:

ModuleNotFoundError: No module named 'bs4'

回答1:


I also faced same issue.

solution which i found best is just add this line to your user or workspace settings(whichever is suits your projects):

"code-runner.executorMap": {
    "python": "C:\\Users\\adarsh_patel\\VisualCode\\env\\Scripts\\activate.bat && python -u",
}

you have to enter your virtualenv path or you could use.

"code-runner.executorMap": {"python":"$pythonPath $fullFileName"}

hope this helps you.




回答2:


A possible solution would be to set the "code-runner.runInTerminal": true in the VS Code settings, which is false by default. Doing so, Code Runner, will run the code in the shell that is configured using the "terminal.integrated.shell.windows" setting.

After that, run your script with Code Runner. This now opens a new terminal, where the python environment you have selected using VS Code's Python: Select Interpreter will be activated automatically, before executing the code. (If the environment is not activated automatically, you can do this also manually, just make sure you do it in the terminal session that was opened by Code Runner.)




回答3:


First approach:

First, I suggest to set executorMap like this:

  "code-runner.executorMap": {
   "python": "\"$pythonPath\" $fullFileName",
   },

by setting this, every time you change your Python interpreter version in VS Code, code-runner will use the same version to execute your code.

Second approach:

Another method I used before was to use a Shebang code in the first line like this:

#! .\venv\scripts\python.exe

code-runner is compatible with the Shebang command and it will execute your code with the version of Python that you have mentioned in the first line.




回答4:


If you watch this video, you can see the solution at 44.55 min if you are a mac user.

You have to define your $pythonPath. However, you don't have to define $fullFineName. It is already done for you if Code Runner installed

Add this to user setting:

"python.pythonPath": "/Users/danielaaa/miniconda3/envs/tf/bin/python",

"code-runner.executorMap": { "python": "$pythonPath -u $fullFileName"}




回答5:


I added shebang line at the beginning of the file pointing at my venv interpreter location eg.

#!/Users/username/Desktop/venv/bin/python

Code runner seems to work just fine.




回答6:


I activated CodeRunner and ran into all the same problems mentioned above.

I then proceeded to pip install requests the module that in my case was present in the venv but not globally, even though it was already present and should have been working in theory. Lo and behold, it now works fine.

I guess the takeaway is that CodeRunner doesn't pick up midstream if you install it post-create of the venv.




回答7:


add this to your user or workspace settings

"code-runner.executorMap": {
        "python": "source $workspaceRoot/venv/bin/activate && python3 $fullFileName",
    },
"code-runner.runInTerminal": true



回答8:


Appending the workspace directory to PYTHONPATH before running the script worked for me:

  "code-runner.executorMap": {
    "python": "export PYTHONPATH=\"$PYTHONPATH:$workspaceRoot\";python -u $fullFileName",
  }


来源:https://stackoverflow.com/questions/50966876/vs-code-code-runner-doesnt-work-with-virtualenvs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!