0

I'm currently trying to execute a bunch of .py and .js files sequentially from one main py script. I use os.system("COMMAND") for that. This works like a charm except for one script which uses numpy and pandas. This of cause throws and exception as native os does not know numpy or pandas.

What options do I have to get those dependencies? Calling something like conda activate someEnv does not feel like the right approach.

Nicolai
  • 62
  • 5
  • You need the dependencies installed in the python environment that you are running. If you are not using virtual environments when running your scripts, then you might want to install the packages to global python. – juhat Apr 07 '22 at 16:54
  • The usage of [os.system](https://docs.python.org/3/library/os.html#os.system) is deprecated since many years and should not be used anymore in newly coded Python scripts. Please use a function of the [subprocess module](https://docs.python.org/3/library/subprocess.html) to run other executables from within a Python script executed by `python.exe` or `pythonw.exe` which gives the Python script programmer full control over the execution behavior. – Mofi Apr 07 '22 at 16:56
  • It is in general also a bad idea to install a bunch of *.py files, let the user run one `.py` file with `python.exe` of the user´s choice from perhaps installed multiple versions of Python, use `os.system()` to run `cmd.exe` with the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) using the environment variables `PATHEXT` and `PATH` to find itself a `python.exe` file which might be of a different version to use `CreateProcess` to run this `python.exe` with the other `.py` file as argument. – Mofi Apr 07 '22 at 17:04
  • There is [sys.executable](https://docs.python.org/3/library/sys.html#sys.executable) giving the absolute path of the executable binary for the Python interpreter which is interpreting the running .py script file. This `python.exe` should be used with one of the `subprocess` module functions. Please read the referenced documentation pages and you should get the knowledge on how to use `subprocess.Popen` without or with the `env` parameter to run the executable with `CreateProcess` with same environment variables as the currently running `python.exe` or with some variables defined different. – Mofi Apr 07 '22 at 17:10
  • The __batch__ file `conda` does on running it with `cmd.exe` using `conda activate someEnv` nothing else than (re)definining some environment variables. Run in an opened command prompt window first `set >Variables_1.txt`, next `conda activate someEnv`, then `set >Variables_2.txt` and finally `fc Variables_1.txt Variables_2.txt` and there can be seen which environment variables are added or modified by the batch file `conda`. Then it should be no problem to define the environment variables inside the Python script without usage of __batch__ file `conda` which is not working within a .py file. – Mofi Apr 07 '22 at 17:16
  • Read the chapter __When is a system or user PATH change applied to processes?__ in my answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) Then you should have the knowledge that no process can modify directly the environment variables of any other already running process as that would require getting access to the memory used by another process. That is of course definitely neither wanted nor possible nowadays for security reasons. – Mofi Apr 07 '22 at 17:20

0 Answers0