question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Directly run a module with relative imports inside a package

See original GitHub issue

The python interpreter got the -m option which allows to directly run a module inside a package . Would be great if PTVS implemented the same so that we can just click on any file inside a package and run/debug it directly.

startapp.py
pkg/
  __init__.py
  components/
    core.py
    __init__.py
  tests/
    core_test.py
    __init__.py

To give a quick example. Let’s say we want to run core.py. In PTVS I have to set startapp.py as my start-up script which then in turn imports pkg.components and then runs core.py.

But on the command line with “python -m pkg.components.core” it can be run directly. So wouldn’t it be great if PTVS supported this as well so we don’t get anymore “ValueError: Attempted relative import in non-package” errors?

Thanks! Patrick

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bmaisonncommented, Nov 9, 2017

Hi, Do you have an ETA for this improvement ? Python 3 forces usage of a new standard notation (https://www.python.org/dev/peps/pep-0328/). We cannot use PTVS in this case. Regards, Bertrand

0reactions
patrickwolfcommented, Nov 3, 2021

@greazer why was this closed without a comment?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Relative imports for the billionth time
Relative imports use the module's name to determine where it is in a package. When you use a relative import like from .....
Read more >
Absolute vs Relative Imports in Python
A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There...
Read more >
Relative Imports in Python 3
Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package.
Read more >
Absolute and Relative Imports in Python
Relative import specifies an object or module imported from its current location, that is the location where import statement resides. There two ...
Read more >
5. The import system — Python 3.11.4 documentation
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found