Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Launching module with --nodebug breaks imports #745

Closed
ghost opened this issue Aug 10, 2018 · 10 comments
Closed

Launching module with --nodebug breaks imports #745

ghost opened this issue Aug 10, 2018 · 10 comments
Assignees
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Aug 10, 2018

My project structure is

demo/
  foo/
    __main__.py
    bar.py

bar.py is an empty file. __main__.py contains the single statement import bar. My launch.json configuration is

{
    "name": "Demo",
    "type": "pythonExperimental",
    "request": "launch",
    "program": "${workspaceFolder}/foo",
    "console": "integratedTerminal"
}

Running in debug mode causes the program to immediately terminate successfully, as expected. With --nodebug the program crashes with a ModuleNotFoundError exception complaining that there is no module named bar.

I am running ptvsd in VSCode with Python3.6 on Ubuntu 18.04.

@ghost ghost changed the title Launching module with --nodebug changes import behaviour Launching module with --nodebug breaks imports Aug 10, 2018
@karthiknadig
Copy link
Member

Can you try this configuration?

        {
            "name": "Module: foo",
            "type": "pythonExperimental",
            "request": "launch",
            "console": "integratedTerminal",
            "module": "foo",
            "cwd": "${workspaceFolder}",
            "env": {"PYTHONPATH": "${workspaceFolder}"}
        },

@ghost
Copy link
Author

ghost commented Aug 14, 2018

Fails with the same error.

@karthiknadig
Copy link
Member

karthiknadig commented Aug 14, 2018

Can you try it with an empty __init__.py file? You currently have __main__.py, and bar.py. Add a __init__.py.

@ghost
Copy link
Author

ghost commented Aug 14, 2018

I did, same issue.

@karthiknadig
Copy link
Member

Can you tell me the version of ptvsd? I am not seeing this in master. But I want to make sure we have addressed this.

@ghost
Copy link
Author

ghost commented Aug 17, 2018

I'm using 4.1.1a10, which ships with the current version of vscode.

Actually I'd like to clarify that the configuration you gave above (with module set instead of program) actually fails with and without --nodebug. Presumably this happens since it's trying to run something like python -m ptvsd -m foo which should fail since bar needs to be a relative import. But with the original configurations vscode runs something like python -m ptvsd /path/to/foo which works fine, but python -m ptvsd --nodebug /path/to/foo fails with the error mention in the OP.

To add some motivation for this bug, I found it when working on a Django app, which internally uses some wonky imports that break when running ptvsd in nodebug mode.

@karthiknadig
Copy link
Member

With the configuration i gave it should work if you have __init__.py. However, there seems to be a issue with namespace packages, in that case too. I will look into this.

@karthiknadig karthiknadig self-assigned this Aug 17, 2018
@fabioz
Copy link
Contributor

fabioz commented Aug 21, 2018

This is not a bug -- things are working as expected.

i.e.: if you have a structure such as the one below where demo is the folder in the PYTHONPATH, you should write an import on __main__.py as from foo import bar or from . import bar (a single import bar expects the bar module to be on the root -- which is not the case).

demo/
  foo/
    __main__.py
    bar.py

As a note, I suggest you don't use relative imports on this case, only full imports (because if it's a relative import it won't work unless python is executed with the -m flag -- when executed passing the __main__.py file itself it'd fail).

@ghost
Copy link
Author

ghost commented Aug 21, 2018

Regardless of how you feel imports should be done, the fact remains that python foo will execute the code without throwing an exception, as will python -m ptvsd foo. Passing the --nodebug option should break this behaviour.

Incidentally, I agree that import bar is a poor way to perform the import. However, projects like Django will perform imports this way e.g. when setting DJANGO_SETTINGS_MODULE, so this bug will break programs that ought to work.

@fabioz
Copy link
Contributor

fabioz commented Aug 21, 2018

Note that it'll fail in the command line if python -m foo is used in plain Python, so, ideally you wouldn't write imports that way, but I agree with your point that ptvsd should replicate what Python does even if it means shooting yourself in the foot ;)

-- Python itself appears to be executing foo/__main__.py directly (so, it'll find bar as a root module -- and if you do in another place an from foo import bar you'll have another version of the same module).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants