Atualmente, estou tentando configurar o Visual Studio Code no Mac OSX 10.13.6 para a codificação python3
. Eu gostaria de evitar o uso de vários ambientes virtuais para meus python3
scripts diferentes e, em vez disso, fazer com que todos sejam executados usando:
(1) a mesma homebrew
instalação depython3
(2) acessar pacotes Python instalados em:
homebrew
lista de pacotespip3
lista de pacotes instaladospip
lista de pacotes instalados.Primeiro, instalei primeiro python3
usando homebrew
:
$ brew info python
python: stable 3.7.7 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.7.7 (4,062 files, 62.4MB)
...
Python has been installed as
/usr/local/bin/python3
...
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
Em segundo lugar, instalei meus pacotes necessários usando homebrew
:
$ brew list
cmake libffi p11-kit
dcraw libheif pandoc
dlib libidn2 pcre
...
jasper numpy webp
...
E outros pacotes usando pip
e pip3
:
$ pip list
DEPRECATION:...
Package Version
-------------------------------------- --------
altgraph 0.10.2
...
numpy 1.8.0rc1
...
zope.interface 4.1.1
$
$ pip3 list
Package Version
------------------ -------
appnope 0.1.0
...
numpy 1.18.2
pandocfilters 1.4.2
parso 0.5.2
pexpect 4.7.0
pickleshare 0.7.5
pip 20.0.2
pomegranate 0.12.2
...
scipy 1.4.1
Terceiro, abri o código do Visual Studio e em "Preferências" -> "Configurações" e configurei "Python:Python Path"
a homebrew
python3
instalação conforme observado acima /usr/local/bin/python3
.
Em seguida, adicionei /usr/local/lib/python3.7/site-packages
por homebrew
instalação python3
ao arquivo de configurações de código do Visual Studio usando:
"python.autoComplete.extraPaths": [
"/usr/local/lib/python3.7/site-packages" ]
Por fim, selecionei meu interpretador Python no Visual Studio Code /usr/local/bin/python3
e tentei executar as 2 linhas de importação a seguir em um .py
script, conforme a captura de tela abaixo. Observe que o interpretador é Python 3.7.0 64-bit
fornecido pelo canto inferior esquerdo da janela do VS Code.
E depois de tudo isso, acabou ficando com essa saída após clicar no botão "Play" para executar o código no canto superior direito do VS Code:
[Running] python -u "/Users/...bayes_net_nodes.py"
Traceback (most recent call last):
File "/Users/...bayes_net_nodes.py", line 1, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'
[Done] exited with code=1 in 0.037 seconds
What would be the most simple way to configure VS Code so I can run python3
scripts that have access to the all the packages I've installed across my system without using virtual environments? Thank you!
Note: One workaround that seems to work, and I'm not sure why is if I put a shebang at the top of my script #! /usr/local/bin/python3
and my output then looks like this:
[Running] /usr/local/bin/python3 "/Users/...bayes_net_nodes.py"
[Done] exited with code=0 in 0.051 seconds
Which is odd, because that's different than the output above where I didn't use the shebang but both python interpreters according to VSCode are indeed /usr/local/bin/python3
I was able to reproduce your problem.. but only when I use Code Runner to run.
Those kind of Output logs with [Running]
and [Done]
is Code Runner.
The play button is also not green, indicating Code Runner because the default is green.
Now, for the fix!
You'll notice that it executed your script using python -u
. That python
would be whatever python
means on your system, which for me is the default Python 2.7. Basically, it's not your Homebrew Python3 with numpy.
Code Runner has a default set of "executors" which tells it which executable to use for which language. Search it for in your settings as "code-runner Executor Map":
Open your settings.json, enter code-runner.executorMap
, then let it auto-complete with the default. You'll then see a long list of mappings between language and executor. Look for the one for python
:
"code-runner.executorMap": {
"javascript": "node",
...
"python": "python -u",
"perl": "perl",
...
}
And there it is: python -u
, the same one it used to run your script.
If you want to continue using Code Runner, simply change it to the whichever python
interpreter you want to use. In your case, it should be /usr/local/bin/python3
:
"code-runner.executorMap": {
...
"python": "/usr/local/bin/python3",
...
}
It should now work:
The reason it works with a #! /usr/local/bin/python3
shebang is because Code Runner has a setting that it respects the file's shebang (code-runner.respectShebang
) which is true by default.
Se você não quiser essa etapa extra de configuração do Code Runner, pode simplesmente desabilitá-lo (ou desinstalá-lo). Todas as etapas que você já realizou (configuração python.pythonPath
, seleção do intérprete e clique no botão Play) já teriam funcionado perfeitamente com a extensão Python da Microsoft . Veja os documentos oficiais sobre a execução de arquivos Python , seleção de ambientes e depuração .
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras