7

I am currently trying to setup Visual Studio Code on Mac OSX 10.13.6 for coding in python3. I'd like to avoid using multiple virtual environments for my different python3 scripts and instead have them all run using:

(1) the same homebrew installation of python3

(2) accessing installed python packages in:

  • homebrew packages list
  • pip3 installed package list
  • pip installed packages list.

First, I first installed python3 using 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

Second, I installed my required packages using homebrew:

$ brew list
cmake           libffi          p11-kit
dcraw           libheif         pandoc
dlib            libidn2         pcre
...
jasper          numpy           webp
...

And other packages using pip and 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  

Third, I opened Visual Studio Code and in "Preferences" -> "Settings" and set "Python:Python Path" to the homebrew python3 installation as noted above /usr/local/bin/python3.

See this screenshot: Python:Python Path

Next, I added /usr/local/lib/python3.7/site-packages per the homebrew install of python3 to the Visual Studio Code Settings file using:

"python.autoComplete.extraPaths": [
    "/usr/local/lib/python3.7/site-packages" ]

Finally, I selected my python interpreter in Visual Studio Code as /usr/local/bin/python3 and tried to run the following 2-lines of imports in a .py script as per the screenshot below. Note that the interpreter is Python 3.7.0 64-bit given by the bottom left corner of the VS Code window.

VS Code Screenshot

And after all of that, ended up with this output after clicking the "Play" button to run the code in the top right corner of 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

4
  • 1
    When you do /usr/local/bin/python3 -m pip list, do you see numpy? (Yes I know you listed your pip3 list, but I just want to confirm that Homebrew's python3 is where that numpy is really installed.) Commented Apr 8, 2020 at 4:56
  • Confirmed, definitely listed. Commented Apr 8, 2020 at 18:55
  • From your console logs, are you using Code Runner? Do you have it installed? Commented Apr 8, 2020 at 23:42
  • 1
    The coderunner part was from running without saving. Updated the console logs to show what happens when trying to run a saved program. Same error except no coderunner. Commented Apr 9, 2020 at 3:13

1 Answer 1

3

I was able to reproduce your problem.. but only when I use Code Runner to run.

enter image description here

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":

enter image description here

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:

enter image description here

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.

If you don't want this extra step of setting-up Code Runner, you can simply disable (or uninstall it). All the steps you already did (setting python.pythonPath, selecting the interpreter, and clicking that Play button) would have already worked just fine with Microsoft's Python extension. See the official docs on running Python files, selecting environments, and debugging.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.