The doc says
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line
I wrote this code to help myself understand the -m
def print_hi(n=1):
print('hi'*n)
if __name__ == "__main__":
print_hi(9)
I saved this as a file aname.py.
And then I ran this command, and get the expected result.
$ python aname.py
hihihihihihihihihi
question
When I execute the file as a module, this error shows up
$ python -m aname.py
/usr/bin/python: Error while finding module specification for 'aname.py' (AttributeError: module 'aname' has no attribute '__path__')
What causes this error? How to fix it?
You're supposed to use the m without the .py suffix, i.e.: $ python -m aname
From the man page:
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as
a script.
The m parameter is a module, similar to import or from.
Related
When I try to run the main script file of a Python package, from Windows cmd prompt, I get Python errors.
I downloaded a folder from github, that contains a multi-folder Python package, to a folder zzz\ on my Windows 10 computer. The package's top folder is OJWALCH_sleep_classifiers.
To run the package, I must run this module:
zzz\OJWALCH_sleep_classifiers\source\preprocessing\preprocessing_runner.PY
I can't figure out how without error.
I opened a cmd prompt and did cd into zzz\OJWALCH_sleep_classifiers.
Here is what I've tried:
1st attempt...
At cmd line: python -m path and name of module
python -m source\preprocessing\preprocessing_runner
ERROR:
C:\Users\Doug\AppData\Local\Programs\Python\Python37\python.exe: No module named source\preprocessing\preprocessing_runner
2nd attempt...
At cmd line: python <path and name of module>.py
python
python source\preprocessing\preprocessing_runner.PY
ERROR:
Traceback (most recent call last):
File "source\preprocessing\preprocessing_runner.PY", line 3, in <module>
from source.analysis.figures.data_plot_builder import DataPlotBuilder
ModuleNotFoundError: No module named 'source'
3rd attempt... CAN'T FIND MODULE: preprocessing_runner
At cmd line: python <path and name of module>
python source\preprocessing\preprocessing_runner
ERROR:
(null): can't open file 'source\preprocessing\preprocessing_runner': [Errno 2] No such file or directory
At cmd line: python -m path and name of module
This way, because you are giving the name directly to Python, you should use . between folder names, and no file name extension - because you are naming a package and module, not a path and file. So:
python -m source.preprocessing.preprocessing_runner
At cmd line: python .py
This would have worked, except that you tried to start Python first, and then give a command-line command to Python, instead of giving it to the command line.
At cmd line: python
Since this is a single command that starts up Python, we are giving it a path and file. So there should now be a .py extension:
python source\preprocessing\preprocessing_runner.py
We can also use forward slashes at the command prompt - it will break tab-completion, though.
So I have a python script which I can call like this (if in the same directory):
python -m fooscriptdirectory
It works just fine, it gives me the output.
But if I try to run this from somewhere else with a full path in the terminal like so:
python -m /home/user/public_html/fooscriptdirectory
I get: No module named /home/user/public_html/fooscriptdirectory
Why is that? It is exactly the same command right? Am I missing something here?
I'm currently learning unittesting, and I have stumbled upon a strange error:
If I run my script from inside PyCharm, everything works perfectly. If I run it from my cmd.exe (as administrator), I get the following error:
This is my code:
import unittest
class TutorialUnittest(unittest.TestCase):
def test_add(self):
self.assertEqual(23,23)
self.assertNotEqual(11,12)
# function for raising errors.
def test_raise(self):
with self.assertRaises(Exception):
raise Exception`
Just remove the .py extension.
You are running your tests using the -m command-line flag. The Python documentation will tell you more about it, just check out this link.
In a word, the -m option let you run a module, in your case the unittest module. This module expect to receive a module path or a class path following the Python format for module path (using dots). For example, if you want to run the FirstTest class in the mytests module in a mypackage folder you would use the following command line:
python -m unittest mypackage.mytests.FirstTest
Assuming that you are running the previous command line from the parent folder of mypackage. This allows you to select precisely the tests you want to run (even inside a module).
When you add the .py extension, unittest is looking for a py object (like a module or a class) inside the last element of the module path you gave but, yet this object does not exist. This is exactly what your terminal error tells:
AttributeError: ’module’ object has no attribute ’py’
you can add at the bottom of your script:
if __name__ == "__main__":
unittest.main()
Then you can run python test_my_function.py normally
when I was in Powershell interface, I ran these command:
PS C:\Users\administrator.HYPERV> Import-Module FailoverClusters
PS C:\Users\administrator.HYPERV> get-module -listAvailable
I can see module FailoverClusters in result.
But when I ran Python code:
>>> os.system(r"powershell import-module FailoverClusters")
It gives me this:
import-module:The specified module "FailoverClusters" was not loaded
because no valid module file was found in any module directory
and then I ran
>>> os.system(r"powershell get-module -listAvailable")
module FailoverClusters didn`t show in the result.
How can I get these python code work as I expected?
Suppose there is a module somewhere, which I can import with
from sound.effects import echo
How can I run echo directly from command line?
Command
python sound/effects/echo.py
does not work since the relative path is generally incorrect
If the module has top-level code executing on import, you can use the -m switch to run it from the command line (using Python attribute notation):
python -m sound.effect.echo
The module is then executed as a script, so a if __name__ == '__main__': guard will pass. See for example the timeit module, which executes the timeit.main() function when run from the command line like this.