0

How can we use python run its script in line ? e.g.

from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script("test-script.py")
for name, mod in finder.modules.items():
   print(name)

Have tried such its one line:

$ python -c 'from modulefinder import ModuleFinder; finder = ModuleFinder(); finder.run_script("/usr/bin/xbacklight"); for name, mod in finder.modules.items(): print(name)'
  File "<string>", line 1
    from modulefinder import ModuleFinder; finder = ModuleFinder(); finder.run_script("/usr/bin/xbacklight"); for name, mod in finder.modules.items(): print(name)
                                                                                                              ^^^
SyntaxError: invalid syntax

How to do it finely working? Thanks before

1
  • Out of curiosity, why would you like to do this? The PEP 8 guidelines suggest a maximum line length of 79 characters as a standard (See this question and associated answers for more in-depth discussion and arguments on why) Jun 23, 2022 at 23:36

2 Answers 2

1

You can wrap your for-each inside a list comprehension in order to make it work using a single-line instruction.

The command becomes

python -c 'from modulefinder import ModuleFinder; finder = ModuleFinder(); finder.run_script("/usr/bin/xbacklight"); [print(name) for name, mod in finder.modules.items()]'
0
1

You can use a here-document. Once you've entered <<something, the shell will pipe all input until it sees "something" again to stdin. Commonly, "EOF" is used.

$ python <<EOF
> from modulefinder import ModuleFinder
> finder = ModuleFinder()
> finder.run_sctript("test-script.py")
> for name, mod in finder.modules.items():
>     print(name)
> EOF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.