Powershell scripts doesn`t function well when running in Python - python

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?

Related

Getting "No module named pabot.__main__; 'pabot' is a package and cannot be directly executed" error, when I try to do parallel execution using pabot

I have created a robot script and tried to run the scripts in parallel using "pabot" package. Im getting the below error message stating that no main.py in the installed package.
Steps that I followed which cause this error.
Installed pabaot using the below cmd:
pip install robotframework-pabot
Below is the command I used to execute scripts using pabot
C:\Users\Prakash\PycharmProjects\Prakash_RobotFramework\tests>python -m pabot .
Below is the error message I received while executing the cmd.
C:\Program Files\Python39\python.exe: No module named pabot.main; 'pabot' is a package and cannot be directly executed"
I have checked the pabot installed folder where main.py is not available.
Try the following:
*** Settings ***
Library pabot.pabotLib
Execution:
Split execution to suite files»
pabot --pabotlib [path to tests]
Split execution on test level»
pabot --pabotlib --testlevelsplit [path to tests]
Looks like there is a pabot and pabotlib class within pabot. Use the following command to call pabot class within the pabot module.
python -m pabot.pabot --testlevelsplit .

Shell command from Python "env: node: No such file or directory"

I got a Node.js CLI program called meyda installed (Mac OS 10.14) using:
sudo npm install --global meyda
From the Terminal I can call the program and it works as expected; like:
meyda --bs=256 --o=apagodis2.csv DczN6842.wav rms
Now, I want to call it from inside a python script (using Spyder) at the same location and tried this – but getting error:
import os
os.system ('/usr/local/bin/meyda --bs=256 --o=apagodis4.csv samples_training/DczN6842.wav rms')
>>> env: node: No such file or directory
I can issue more "traditional" shell commands like this from the same Python script and it works:
os.system ('cp samples_training/DczN6842.wav copy.wav')
Also tried subprocess call with same result. I confirmed the executable is at /usr/local/bin/
To make sure I also removed all file arguments calling the program using only the help flag but same, error.
os.system ('/usr/local/bin/meyda -h')
>>> env: node: No such file or directory
Why is the command not found from inside Python but sucessfully in the macOS Terminal?

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

-m command line flag not working when specifying full path to Python script

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?

python -m causes "Error while finding module specification for"

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.

Categories