Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was wondering the purpose of runpy module in Python and how does it help in day to day development of the Python user community?
Can someone please explain the necessity, usage and advantages of runpy module in python?
The docs say:
The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.
You can run a python module like this:
python -m SimpleHTTPServer 8000
The runpy module enables this functionality. It may not be useful outside of this context.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a python file with no argparse implementation in its __main__, yet, I'm interested in having a look at the functions and modules implemented in it from the commandline. I'm tempted to write a function to perform such exploration but I wanted to find out first whether this is already available.
EDIT 1: to make it more concrete, I'm interested in names of functions and classes + their docs.
You may be looking for a tool like python-fire.
From the repository:
Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object. [...] Python Fire helps with exploring existing code or turning other people's code into a CLI.
It is available as a package on pip.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Our product imports the library winreg.
I am writing a test which I would like to mock the entire library.
I do not want to mock specific functions, nor enumerate through all of the functions and mock them. I would like to mock the entire library, such that any instance of the library being imported or used instead uses the mock.
Is this possible?
Have you tried patch?
It is not necessarily intuitive to setup though, Python doc example:
#patch('mymodule.SomeClass')
class MyTest(TestCase):
This is a tricky problem. I think you're best bet is to have your real library "winreg" in one folder location and your mock library in another location and do some sneaky PYTHONPATH manipulation when you run the unit tests.
Folder structure and assuming winreg is a python module but could still work if a folder.
real/winreg.py
mock/winreg.py
When you execute your unit tests if on a Linux system:
PYTHONPATH=<directory to mock>/mock:$PYTHONPATH <command to run unittests>
When you run the normal code flip the PYTHONPATH back to the "real" library. You'll just have to make sure that the real library isn't picked up in your PYTHONPATH when running the unit tests which could be tricky depending on your set up.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It seems to me python -m myscript and python myscript do the same thing: running a script.
What is the purpose of using -m? Thanks.
In some cases, especially for very small projects, python script.py and python -m script will be pretty much the same.
The biggest difference is when your module lives in a package and has relative imports. If you have a script that import something like from .module import some_name, you will most likely get a ModuleNotFoundError when you run it with python package/scripy.py. On the other hand, python -m package.script will produce whatever output you expected.
You can load modules and invoke them as script. The exact file name or path is not needed. Example:
python -mjson.tool myfile.json
This will print a formatted version of myfile.json, and it loads the module json.tool for this. Python searches for this module automatically. You don't need to know the exact path.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a lot of modules to import before running each script. But the modules i am importing is same in all the scripts. In case of debugging i have to change some code in one or more modules and again run the script. So each time python imports all the modules from the starting. Is there anyway that i skip importing of modules that haven't modified?
You can reload particular modules with reload function, like this
reload(math)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using Blender and Python and want to give the .py file to other users to use my script... My script uses external modules (like NumPy) which will rise the errors (like can't find module xxxx).
Not all people can install NumPy (or even Python :D) as many Blender users are just artists.
Another note is that NumPy doesn't work with Blender (I install it in the system's Python, then delete Blender Python so it relies on the system Python).
If you want to distribute your code with external dependencies then you should build a Python egg. The .egg format was created to solve the issue you are dealing with. It is a self-contained release of your code with dependencies and meta-data. Here is some information on how create Python eggs.