0

From what I found in the community, there are two options to extend the Python search path with further libraries.

  • .env e.g. PYTHONPATH="C:\path\to\a;C:\path\to\b"
  • launch.json e.g.
{
    // 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: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "PYTHONPATH": "${workspaceFolder}/lib"
            }
        }
    ]
  }

After searching again for this topic on multiple days, I did not yet find any clear solution for that. Some things work as expected, others don't. To be clear, it might be just a wrong understanding of the concepts. Since I found multipe threads on the topic, but almost just the same answers, which did not yet work out for me. There are multiple threads aroung that stuff, so I assume some general interest for a clarification.

Currently I tested exlusively each option. Using both things in parallel is somehow not comfortable, also since the capabilities are different. .env does not support pre-defined variables, lauch.json, does. So I tested .env without settings in launch.json and no .env file + settings in launch.json.

I use VS Code 1.75.1, but it was the case also for multiple previous version. Anaconda Python3 with different virtual environments.

What I see is the following, when using the options in VS Code:

yes --> extending the PYHTONPATH worked as expected no --> extending the PYHTONPATH dir NOT worked as expected

Run option .env setting launch.json setting
Run Python file in terminal no no
Run in interactive Window yes no
Debug via F5 yes yes
"Debug Python File" (*) yes no

(*) Debug via "Debug Python File" in Drop-Down of Run Symbol above right of the editor window

I assumed both debug options should be the same, but I have sometimes situations, where starting Debug via F5 works as expected, but using the Debug Symbol reports an error.

Not clear, why both debug options behave differently. No idea, why running Python terminal, does ignore path settings completely. In general more detailed information about the ideas behind both optios, would be very helpful. Why there is a difference for just running code in the terminal and debugging. The concept of launch configurations I know from other IDEs and it makes a lot of sense to me. Is there a way define such conigurations hiarchically, so only changes have to be added for lower levels and the rest can be kept common.

Setting all environment stuff on OS/command shell level, is possible via system/user variables. But I thought, that VS Code is doing that specifically for each workspace/project.

What am I doing wrong? What do have I missed during setup of VS Code? Or where do I not yet understand the concecept/workflow correctly.

I expected to control the library path settings for each workspace seperately. Potenitally in just one place (one configuration setting) in the workspace area (.env or launch.jason, or what ever other way)

Any help/comment is appreciated, thanks a lot,

Mike

Mike M.
  • 3
  • 3
  • I'm confused in what you're actually trying to achieve by doing this? What is the goal? You can use different versions of python for different workspaces: In the bottom right of the screen, press on python or interpreter or similar to that, and select workspace, and which python version/path to use – mrblue6 Feb 16 '23 at 23:41
  • The major question is, how to handle a non-managed library (user code, not included in the virtual environment, but shared throughout workspaces) to be used in VS Code correctly (for Debug and also run in Python interpeter)? There are ways to extend the search path as described in my question, but they do not work as I expect it. – Mike M. Feb 17 '23 at 09:35

1 Answers1

0

For the questions raised in your article, if you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File.

enter image description here

If you're looking to debug a web application using Flask, Django or FastAPI, the Python extension provides dynamically created debug configurations based on your project structure under the Show all automatic debug configurations option, through the Run and Debug view(f5).

enter image description here

You can read document basic-debugging for more detials.

I expected to control the library path settings for each workspace seperately.

I suggest you using virtual environment. This makes it easier for you to manage your library.

In addition, if you want to import the package in the specified location, you can use env in the launch.json file:

  "env": {
    "PYTHONPATH": "path/to/your/package"
  }

Another way is using .env file. Add the following codes to your settings.json then create .env file under project folder:

"python.envFile": "${workspaceFolder}/.env",

.env:

PYTHONPATH=path/to/your/package

Then you can use Debug Python File button.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • Debugging itself works fine both ways (Symbol or F5). My concern is, how to provide a user code library, not beeing handled by the package manager. Debugger and Python interpreter need to be aware of this library. When running the code outside of the IDE, we extend the search in the shell environment. Most other IDEs I used in the past like PyCharm, Spyder, Eclipse, have a more or less comfortable way to handle that topic. So the major question is, how to handl a non-managed library to be used in VS Code correctly (for Debug and also run in Python interpeter)? – Mike M. Feb 17 '23 at 09:31
  • @MikeM. I've added my answer. Do you think whether it can meet your needs? – MingJie-MSFT Feb 20 '23 at 02:36
  • Thanks MingJie-MSFT for your additional info. That is right what I am doing, after reading the documentation. I'm wondering, why it is ot working generally. Setting PYTHONPATH in launch.json does only have an effect when debugging via F5. Using "Debug Python File" (*) does not find the library. Running Python in terminal via VS code, nor a interactive (cell based proceessing) does find the library. Using .env file for extending PYTHONPATH behaves different, but also not as expected. No idea if this is a intended behavior, if I'm doing somehting wrong, or ... ? – Mike M. Feb 21 '23 at 04:41
  • BTW, if you want to use `Debug Python File` button, you can read the answer. I added the way as well. – MingJie-MSFT Feb 21 '23 at 09:33
  • Just a view more questions to clarify things: (1.) Why does "Run Python file in Terminal" ignore PYHTONPATH setting in .env file? (2.) Why is "Debug via F5" and "Debug Python File via Drop-Down-Icon behaving different in terms of PYTHONPATH setting in launch.json? (3.) The "launch.json" file is only important for debugging (defining only debug configurations)? How to e.g. run a Pyhohn script with command line parameters directly from the IDE? How to define launch configurations running not in debug mode? – Mike M. Feb 22 '23 at 06:59
  • Can you please comment on the questions from my prvious comment? [Comment: Mike M. Feb 22 at 6:59](https://stackoverflow.com/questions/75470472/how-to-extend-python-search-path-in-visual-studio-code#comment133258327_75479535) – Mike M. Feb 27 '23 at 07:04
  • I'm so sorry that I didn't notice your additional comments. For your first and second questions, I want to say that this is caused by the design. The difference between them is whether to call `launch.json` or not, which can reduce the load of the IDE. For the thied question, `launch.json` file is only for debugging mode. If you want to define launch configurations for normal running mode, could you please specify the requirements? – MingJie-MSFT Feb 27 '23 at 07:11
  • How to e.g. run a Pyhohn script with command line parameters directly from the IDE? You can read this [answer](https://stackoverflow.com/a/2149029/18359438) for help. – MingJie-MSFT Feb 27 '23 at 07:16
  • Thanks again for further clarifcation. To be honest, I never passed arguments to the Python script in VS code up to now. I thought it is possible like e.g. in [Spyder](https://qbi-software.github.io/Python-tutorial/lessons/1-scripting.html). I know also from a colleague, that he is doing that continously in PyCharm and also Spyder. The option to workaround in the script in case no args are passed and define default values there, should work in any case. – Mike M. Feb 28 '23 at 13:31