Robert Oschler
Robert Oschler

Reputation: 14375

Visual Code on Windows can't find module after installing it with PIP. Using wrong Python in Terminal window?

I am using Visual Code 1.47.0 on Windows 8.1 with the latest version of the Python extension installed too. I just installed a module using pip. I then created a new Python file to test it. The IDE highlights the import statement as an error and the drop-down info box says "Unable to import 'extpylib'. If I try to run/debug the test file, I get a traceback error saying:

C:\Users\User\AppData\Roaming\Python\Python38\Scripts

Exception has occurred: ModuleNotFoundError
No module named 'extpylib'
  File "C:\Users\user\Documents\PythonProjects\test-extpylib.py", line 2, in <module>
    import extpylib

If I run the pip install command again, even with the "--upgrade" command line argument, it says everything is installed and that I'm using the latest build. I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit? I am not using the Code Runner extension.

I assumeg that this mismatch of versions between the IDE and the VSCODE Terminal window is causing my issue? If so, how can I get the VSCODE Terminal window to use the same version as that which is selected in the IDE? If not, what is the real problem and how can I fix it?

Upvotes: 2

Views: 13933

Answers (5)

Binita Bharati
Binita Bharati

Reputation: 5898

pip command that was used to install the package is based on a different version of python versus what VS Code is using.

Check the python version of your pip command, run pip --version. For example, on my system, this is the output of this command.

$ pip --version
pip 22.0.4 from /Library/Python/3.8/site-packages/pip (python 3.8)

Check the version of code that VS code is using. For VS code 1.61, the python version will be visible at the bottom left corner while a python file is open in the editor. Screen shot of the same is given below. You can also observe from the screen shot that lazy_streams package is not being recognised by VS code.

enter image description here

To solve the error in VS code, update the python version in VScode to be same as that of pip. Updating of the version can be done by simply clicking on the Python version displayed, which will further prompt you to select among all available python versions on your system.

Upvotes: 0

plmk
plmk

Reputation: 2634

You may have many python paths and VS code is using the wrong one. Choose the right interpreter path just clicking here:

enter image description here

Upvotes: 9

Jie Yin
Jie Yin

Reputation: 706

I solved this by closing and re-opening the vs-code after installing new package.

Upvotes: 3

Steven-MSFT
Steven-MSFT

Reputation: 8411

Of course, the package you installed in an environment is independent of other environments.

As you said the VSCode Terminal using Python2.7 while the status bar shows the selected interpreter is Python3.8.3 64-bit. This meaning you are using the global python(3.8.3) to run the python file while you install the package to the Python(2.7) environment.

Solution:

After you selected the python interpreter in the status bar, you need to activate the environment in Terminal through shortcut 'Ctrl+Shift+`'. You can through these commands to check which environment you are using:

In the terminal:

By command "pip --version" to check which pip you are using.

By command "python" -> "import sys; sys.executable" to check which python you are using.

By command "python" -> "import sys; sys.path" to check which 'site-package' the interpreter searching for.

Upvotes: 0

mabergerx
mabergerx

Reputation: 1213

It seems like an environment issue. In my own Visual Studio code integrated terminal, when I type which python and which pip, I get the correct path to both python and pip executables. So this:

I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit?

Definitely seems to be the issue.

If your VSCODE terminal still uses Python 2.7, you could follow the tips presented here:

However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.

and

Note: conda environments cannot be automatically activated in the integrated terminal if PowerShell is set as the integrated shell. See Integrated terminal - Configuration for how to change the shell.

and

Changing interpreters with the Python: Select Interpreter command doesn't affect terminal panels that are already open.

Upvotes: 0

Related Questions