I have an application that imports a module from PyPI. I want to write unittests for that application’s source code, but I do not want to use the module from PyPI in those tests. I want to mock it entirely (the testing machine will not contain that PyPI module, so any import will fail). Currently, each time I try to
Tag: python-module
Why can’t a module be a context manager (to a ‘with’ statement)?
Suppose we have the following mod.py: and the following use of it: I get an error: According to the documentation the documentation the with statement should execute as follows (I believe it fails at step 2 and therefore truncate the list): The context expression (the expression given in the with_item) is evaluated to obtain a context manager. The context manager’s
Python libraries to calculate human readable filesize from bytes?
I find hurry.filesize very useful but it doesn’t give output in decimal? For example: But later when I add all the values I don’t get the exact sum. For example if the output of hurry.filesize is in 4 variable and each value is 3. If I add them all, I get output as 15. I am looking for alternative of
Unable to import a module that is definitely installed
After installing mechanize, I don’t seem to be able to import it. I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get: The installations I ran previously reported that they had completed successfully, so I expect the import
What’s the difference between __builtin__ and __builtins__?
I was coding today and noticed something. If I open a new interpreter session (IDLE) and check what’s defined with the dir function I get this: Please note the last line. So, my question is: Is any an alias of the other one? Are the Python guys planning to get rid of one of those? What should I use for
Automatically call common initialization code without creating __init__.py file
I have two directories in my project: “src” contains my polished code, and “scripts” contains one-off Python scripts. I would like all the scripts to have “../src” added to their sys.path, so that they can access the modules under the “src” tree. One way to do this is to write a scripts/__init__.py file, with the contents: This works, but has
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.) For