So I am trying to use unittest.mock to mock some of my methods in my unit tests. I do: But I am getting: I tried: It’s still not working. Answer unittest is a built-in module; mock is an external library (pre-3.3 betas, anyway). After installing mock via pip install, you import it not by using but Edit:…
Tag: python-import
Sibling package imports
I’ve tried reading through questions about sibling imports and even the package documentation, but I’ve yet to find an answer. With the following structure: How can the scripts in the examples and tests directories import from the api module and be run from the commandline? Also, I’d like to…
django import error – No module named core.management
Ok, I see plenty of these errors around. I have tried everything I know to do and have yet to figure this out. I am working on a development server running python 2.5 and Django 1.3. Django 1.3 was installed using python setup.py install after unpacking the tar.gz download. All works well, I seldom have the n…
How to import a Python class that is in a directory above?
I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file? Answer from ..subpkg2 import mod Per the Python docs: When inside a package hierarchy, use two dots, as the import statement doc says: When specifying what module to import y…
How do I unload (reload) a Python module?
I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What’s the best way do do this? Answer You can reload a module when it has already been imported by using importlib.reload(): In Python 2, reload was a builtin. In Python 3, it was moved to …
How can I import a module dynamically given the full path?
How do I load a Python module given its full path? Note that the file can be anywhere in the filesystem where the user has access rights. See also: How to import a module given its name as string? Answer For Python 3.5+ use (docs): For Python 3.3 and 3.4 use: (Although this has been deprecated in Python 3.4.)…