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.

Run a module without any functions

See original GitHub issue
  • I have searched the issues of this repo and believe that this is not a duplicate.

Issue

I’m writing a simple one-off script, without any functions, and I’d like to run it through Poetry, in its virtual environment. However, it appears this currently isn’t possible, as Poetry expects a callable:

https://github.com/sdispater/poetry/blob/af8f86cda703144e9261b700d3165433aa2d3603/poetry/console/commands/run.py#L35-L37

My Python is rusty, so maybe there’s a way around this (which makes it a documentation concern), but if there isn’t, I suggest adding a quick heuristic, checking for the presence of a colon, and just executing import_module('{}') instead of import_module('{}').{}() otherwise.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
alecmevcommented, Jul 23, 2018

Have already done that, of course 😄 Not a fan of wasting horizontal space for nothing though. Just checking whether that PR covers my usecase before closing.

1reaction
cauebscommented, Jul 22, 2018

It allows you to specify not just entry points (python functions), but any script, by setting its path.

[tool.poetry.scripts]
main = { path = "main.py" }
~/project $ poetry run main

Edit: yes, this will make that script available when you install this package, but it will also be made available in your environment when you do poetry develop (because this just installs the package in editable mode).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is Python running my module when I import it, and how ...
Put the code inside a function and it won't run until you call the function. You should have a main function in your...
Read more >
6. Modules — Python 3.11.1 documentation
A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only...
Read more >
Importing a module runs code - Python Morsels
When Python imports a module, it runs all the code in that module. ... Then it defines a function called greet , calls...
Read more >
When I import my module in python, it automatically runs all of ...
When I import my module in python, it automatically runs all of the defined functions inside of it. How do I prevent it...
Read more >
31.4. runpy — Locating and executing Python modules
The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m...
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