I am trying to run a python script and I wanted d to run it with a more pythonic way rather than maybe calling subprocess.run().
I found the runpy library and I am trying to call my script like this:
runpy.run_path(path_name='/home/dir1/dr2/dr3/run_code.py', run_name='__main__')
This returns me and error that one of the modules used in the run_code.py does not exist:
ModuleNotFoundError: No module named 'fluent_logger'
When I run the same script with subprocess.run() I get no errors.
Am I using this the wrong way?
Related
I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.
I compiled my python application using pyinstaller and the exe works fine, but when I run it on a different machine without python any part of the code which contains subprocess.Popen()doesn't run.
I read too many questions but I couldn't wrap my head around this.
My popen line:
try:
process = subprocess.Popen(['python', os.path.abspath('about.py')],stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE,
shell=True)
except Exception as e:
print(e)
Note that the executable runs on the host machine, but on another machines it runs but fails when launching the popen.
===UPDATE====
The console shows nothing and doesn't print an exception, so I guess this is a problem with python not being found. How can I fix this?
===UPDATE2====
Following the suggestion of viilpe I used "exec(open..." but it required me to import the about.py module first; importing the module runs it on top of the main module.
Putting exec(open...) inside the try\except runs the main module and the about module alongside each other; ruining the application's GUI.
I'm using "kivy" as my GUI library.
Looks like you want to execute about.py but there is no python.exe in pyinstaller bundle.
As advised here you can do it this way:
exec(open('about.py').read())
The whole point of PyInstaller is to make the destination computer be able to run your script without having a standalone python installation. You can't run a subprocess on a tool which isn't (necessarily) installed.
There are various ways to run Python as a subprocess of itself natively; start by exploring the multiprocessing library.
If the requirement to run Python twice is not a hard one, the absolutely simplest solution is to import about and run the code as part of your script. This probably requires some refactoring of the code in about.py.
I have something like the following structure in my python package:
package/
--functions/
--functionsA.py
--functionsB.py
--utility/
--utils_printing.py
--utils_server.py
My goal is to be able to import package within any of the contained .py files in a way which works when I execute a specific function in one of the .py files from the command line.
Right now, if I want to use a server function in functionsA.py then I just throw a
from package.utility.utils_server import server_function
at the top of functionsA.py. Debugging with PyCharm, this works.
Now I'm trying to use pyheat to show me where my code needs to be optimized, but it requires that I run a given file as a script from the command line, but if I try to run pyheat functionsA.py then I'll get an error like ModuleNotFoundError: No module named 'package'.
When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.
I'm trying to run a python script from a python program by kicking it off from subprocess (The reason is that the main program has to have exited when the script runs, with a combination of wx.CallAfter and Close). However when the script runs I get an error on line 1 with ImportError: No module named os which makes me think it's something to do with the PythonPath, but I can run the script just fine from a terminal.
Why can't the script see any core modules when run this way?
Edit:
The line in question is:
wx.CallAfter(subprocess.Popen,'python %s "%s" %s %s'%(os.path.join(BASE_DIR,"updatecopy.py"),BASE_DIR,pos[0],pos[1]),shell=True)
BASE_DIR is just the directory that the script lives in.
subprocess is there because os.exec* has been deprecated so I wouldn't suggest using that in place of Popen as someone suggested.
I've seen this issue crop up when running from a frozen process. If that is the case then you're most likely inheriting a weird environment for the new python process.
Most frozen scripts will be trying to run from a zip file, in which case it's no wonder that Python can't find anything, it's all trapped in a zip file :)
If this is the situation then try running using the python executable that you are using to run the frozen script. It should be able to deal with the special environment.
Maybe you could use os.execv instead of Popen.
From os/python docs:
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.
(emphasis mine)