2

Environment

cd workspace/playground
python3 -m venv .
source bin/activate
pip install pandas
pip install pycorp2

Running

cd workspace/playground
source bin/activate
py playground.py

I have a Python script that uses Pandas and PyCorp2. Running it via the terminal as per above works. However, I cannot figure out what I have wrong with VS Code.

When I launch VS Code with the Python extensions installed (Python, Pylance, Python Debugger, Python Environments), they don't work. In the notification area I get Refreshing virtual environments, Refreshing Global Python interpreters, Discovering Python Interpreters, and Initializing virtual environments. These messages do not go away.

I do not see any errors in the Output window for Python, Python Debugger, or Python Environments.

What am I missing? If I try to run the file in the Run and Debug area it also doesn't produce anything and I just see a blue bar that doesn't ever stop (similar to the notifications).

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

settings.json

{
    "python.useEnvironmentsExtension": true,
    "python.defaultInterpreterPath": "/home/<me>/workspace/playground/bin/python3"
}

The same thing happens if I switch the interpreter to /usr/bin/python3 and remove the virtual environment that was created. It feels like to me VS Code isn't finding Python.

2 Answers 2

3

I figured it out. There was something wrong with the ~/.vscode-server. I found some window that showed a message about a Python locator and it pointed to the ~/.vscode-server directory. It didn't have an error, but I wiped the directory out and it's now working after re-installing the extensions. I recommend doing a listing of your extensions so you know what you had installed before wiping.

# Close VSCode
ls ~/.vscode-server/extensions
mv ~/.vscode-server ~/.vscode-server-bak
# Relaunch VSCode
# Reinstall extensions (Python, Python Debugger, Python Environments)
# I can now launch the current file using the launch.json in the OP
Sign up to request clarification or add additional context in comments.

Comments

1

Check Interpreter

The easiest way determine which python interpreted is being used is to import sys and show/print the path.

import sys
print(sys.executable)

### Mine shows the below
# '/opt/anaconda3/bin/python'

Remove and rebuild venv

This setup also likely has namespace issues related to the setup.

With venv being set up directly in the playground directory by cd workspace/playground; python3 -m venv ., the venv parent directory is playground. The name of the file above is playground.py within that same playground directory. It also means any files in playground/ can interfere with other imports (e.g., a custom pandas.py will prevent import of the full pandas package). The information is not enough to say exactly what is happening, but these kinds of issues can create odd and hard to diagnose behaviors.

I suggest, moving your venv to another location, outside of playground to avoid this and using a specific directory name when creating for clarity. Below example creates a new virt_envs dir at the workspace level and names this env as .playground_env.

cd workspace
mkdir virt_envs
cd virt_envs
python3 -m venv .playground_env
source .playground_env/bin/activate
pip install pandas
pip install pycorp2

Then to run

cd workspace
source playground_env/bin/activate
py playground/playground.py

Update settings.json to match.

{
    "python.useEnvironmentsExtension": true,
    "python.defaultInterpreterPath": "/home/<me>/workspace/virt_envs/.playground/bin/python3"
}

Additional Help in VS Code

See this post on setting an interpreter in VS-Code it includes the approach mentioned above but also a drop-down selection approach that may help.

1 Comment

I couldn't even get a file to run so the import and print wouldn't work. I don't know what exactly was wrong, but, wiping out the .vscode-server directory fixed the issue after re-installing the extensions.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.