Tools like Github Copilot can generate/complete code and/or comments while typing within an IDE. It looks like they are limited to this use case.
However, I actually want to something similar to the following in any Python program:
import coolcopilotpackage as cop
comment = cop.generate("print('Hello World!') #")
print(comment)
The output should be looking like: Prints the line 'Hello World!', completing the line of code that I started within the string given to cop.generate.
Is there any way of doing that?
If I understand correctly you are looking for an API giving you access to a generative AI. Maybe take a look at OpenAI's API, they have a lot of models you can use in your own projects to build your own tools.