VSCode cannot find installed packages

I use VSCode in an interactive online session as explained here:

I have installed packages using pip install -m requirements.txt, and am now trying to import these packages in a python script called MyScript.py. When running this python script from a command terminal using python MyScript.py, everything works fine.
But when I start the script from within VSCode (“Run/Debug python file in terminal”), it cannot find the module (No module named ‘XYZ’).

Any ideas?
Also, clicking the green “Start debugging (F5)” arrow does not follow the currently selected Debug configuration from .vscode/launch.json but ask me everytime to “Select a Debug Configuration”. What is up with that?

Hi @dennis-beermann, thanks for reaching out! My first instinct is to double-check that you are using the correct python environment when running the script in VSCode - it doesn’t always select the correct one per default. If this is a project you can share, I’m happy to take a look.

However, if you are already a VSCode user you might find it nicer to work in VSCode through a remote SSH connection to your session. The documentation on how to do this is here.

First of all thanks for the input! I have not set up any python environments, so I would be grateful if you could take a look. The project is just barebones at the moment, I literally only added the imported module and a script trying to call it:

The local-IDE-path you described sounds nice as well. However, I have to sort out some WSL issues on my side before I can try that.

Sorry for taking a while to check on this @dennis-beermann - I just ran your project and it seems to be working fine, did you figure out the issue in the meantime?

Hi @rrrrrok,
first of all thanks a lot for checking out my probem. But as I mentioned before, the problem is not running it from the terminal, but with vscode’s Run and Debug mode.

Hi @dennis-beermann ! Have you tried creating a virtual environment as explained here? I happened to face similar issues in the past and everything was solved when working from a virtual environment in VSCode. I have tested the solution locally in my computer and it works:

Do not doubt to reach me if you have any further question.

Apologies, of course, that was the whole point of your question :man_facepalming:

Just went back to check it again, and indeed by default it is using the system python instead of the python in the conda installation (which is where all of your packages get installed).

At the bottom of the window, there is a statusbar - one of the elements there is the python interpreter.

If you click on it, you can change it to the conda one (which it highlights as “Recommended” - not sure why it’s not automatically selected!). Then the python tooling should work correctly for your project.

Btw, I realized that there is now an easier way to install the code-server. Instead of modifying the Dockerfile, you can simply change the environment.yml file like this:

name: "base"
  - conda-forge
  - code-server >=3.2
  - jupyter-vscode-proxy
prefix: "/opt/conda"

It should be easier to stay up-to-date with new releases that way! Hope that helps.

Thanks @elisabetc and @rrrrrok for your answers. I know about venvs but my main issue was that I could not locate a way to select the interpreter. This very much solves my initial issue!!

One last thing since you brought it up: I have update the environment.yaml as suggested and taken out the code-server line in the Dockerfile, then commited my changes. Works great! However, if I do the same on a new repo (Reproducible Data Science | Open Research | Renku), how do I actually get a link to the VS Code notepad in the Launcher?

Glad to hear that solved the problem!

Regarding the launcher - I’m not sure, installing the extension should configure it automatically… I’ll try to debug a bit