python import multiple times

As described in python documentation, when python see some import statement it does the following things: checks some global table if module is already imported if module is not imported python imports it, creates module object and puts newly created module object to the global table if module is imported python just gets module object … Read more

How can I use functools.singledispatch with instance methods?

Update: As of Python 3.8, functools.singledispatchmethod allows single dispatch on methods, classmethods, abstractmethods, and staticmethods. For older Python versions, see the rest of this answer. Looking at the source for singledispatch, we can see that the decorator returns a function wrapper(), which selects a function to call from those registered based on the type of … Read more

Python type hinting without cyclic imports

There isn’t a hugely elegant way to handle import cycles in general, I’m afraid. Your choices are to either redesign your code to remove the cyclic dependency, or if it isn’t feasible, do something like this: # some_file.py from typing import TYPE_CHECKING if TYPE_CHECKING: from main import Main class MyObject(object): def func2(self, some_param: ‘Main’): … … Read more

OSError: [WinError 193] %1 is not a valid Win32 application

The error is pretty clear. The file hello.py is not an executable file. You need to specify the executable: subprocess.call([‘python.exe’, ‘hello.py’, ‘htmlfilename.htm’]) You’ll need python.exe to be visible on the search path, or you could pass the full path to the executable file that is running the calling script: import sys subprocess.call([sys.executable, ‘hello.py’, ‘htmlfilename.htm’])

How could I use requests in asyncio?

To use requests (or any other blocking libraries) with asyncio, you can use BaseEventLoop.run_in_executor to run a function in another thread and yield from it to get the result. For example: import asyncio import requests @asyncio.coroutine def main(): loop = asyncio.get_event_loop() future1 = loop.run_in_executor(None, requests.get, ‘http://www.google.com’) future2 = loop.run_in_executor(None, requests.get, ‘http://www.google.co.uk’) response1 = yield from … Read more