Home > other >  I can't run python modules using VSCode extension, but I can using terminal
I can't run python modules using VSCode extension, but I can using terminal

Time:01-28

I cannot run my python script with imported modules/packages using the VSCode extension for Python, that allows you to run scripts directly from VSCode (the official one from Microsoft). I installed a bunch of packages using pip3 install numpy and the other modules, which I discovered I had to because only "pip" on Mac installs on python 2. Code example below

print('testing')

import numpy as np
import pandas as pd
from openpyxl import Workbook
wb = Workbook()

print('omg this wont work')

When I try to run it using the Run button from VSCode python extension (Microsoft one), it shows up Error as if I didn't had previous installed the modules using pip3.

But when I try to run it on terminal using python3 teste.py it works and imports my modules right.

OS Version: macOS Catalina 10.15.7 python3 --version Python 3.10.2 python --version Python 2.7.16

CodePudding user response:

You should choose which python to using in VSCode. Check this documentation to see how it is done: vscode python environments

CodePudding user response:

For default python environment installed in machine:

  1. Press ctrl shift p (this will open console at top center) For mac press command shift p instead

  2. In the console type Python: Select interpreter (then hit enter)

  3. You can see list of python interpreters as drop-down options or a single interpreter which is installed in your machine.

  4. Select that and then run your code

  5. Before running your py script make sure that you have re launch the editors terminal.

For custom python environment installed in machine

1.Download anaconda and install it on Mac.

  1. Now in step 3 mentioned above, in the drop down you can find conda environment as well select that and relaunch the terminal.

  2. Once conda environment selected in terminal you can see the (conda) will start coming in your terminal in starting of line.

  3. Also if you are using anaconda environment, do install everything as conda install package_name instead of pip install...

  •  Tags:  
  • Related