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) Commented 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()]'
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.