I can shed a bit more light on this issue.
First of all, for many reasons, calling your Python script using QSH is the right thing to do. There are other options (QP2SHELL, QP2SHELL2 and QP2TERM), but they are more specialized and generally more difficult to use. Even though Python is a PASE program, QShell is the environment that is most likely to get all of your I/O where it belongs and properly converted to the correct ccsid, even when using a PASE program.
The difference you get in the environment is based on the QShell handling of the .profile or /etc/profile file where you most likely define your environment variables with something like:
PATH=$PATH:/QOpenSys/pkgs/bin
QSH only sources this file if you are running an interactive shell. By definition, if you specify a command, it is not an interactive shell, so no .profile file processing.
Most Unix shells have alternative files that will run for both interactive and non-interactive shells, but QSH does not.
The simple solution is just to force the .profile (or any file of your choice) to get sourced using the syntax:
QSH CMD('. $HOME/.profile;python3.9 myscriptname.py')
You may have noticed that there is an extra period at the beginning that looks out of place. That tells the Unix shell to SOURCE the file rather than EXECUTE the file. The difference is that sourcing a file just means running the commands in the current job level and context. That means the change to the environment variable occurs in the current process, not a sub-process that would immediately go away taking the newly set environment variable with it.
The $HOME gets expanded by the shell to the current HOME directory of the user, and the semicolon just separates multiple commands to be processed by the shell.
I very much prefer this method to the ADDDENVVAR in the CL program, primarily because it allows the Unix environment to be managed by the Unix environment, and is much easier to include the default PATH without hardcoding it in a CLP.
An alternative is:
QSH CMD('PATH=$PATH:/QOpenSys/pkgs/bin python3.9 myscriptname.py')
That just explicitly sets the path in the command. It is better than the ADDENVVAR approach, but hardcodes a value that, in my opinion, is better softcoded in the environment where it will be run.
One more thing. In my examples, you may have noticed I put the added directory at the end of the path, rather than the beginning. That is because there is some overlap in the QSH and PASE environment binaries. If there are two versions of the binary commands, you typically want the QShell version rather than the PASE version when you are running in the PASE environment.
------------------------------
Vincent Greene
IT Consultant
Technology Services
IBM
Vincent.Greene@ibm.comThe postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
------------------------------
Original Message:
Sent: Tue January 24, 2023 07:05 AM
From: Paulo Silva
Subject: $PATH on a CLP?
Dear Satid,
Thanks a lot for your help.
I have my user $PATH defined correctly and I was expecting that to be enough to run QSH commands.
From what I see that's not the case and it is still mandatory to define an environment variable on the CLP as suggested by Scott Klement.
That's what I did.
Thanks again Satid!
BR,
Paulo
------------------------------
Paulo Silva
Original Message:
Sent: Mon January 23, 2023 10:20 PM
From: Satid Singkorapoom
Subject: $PATH on a CLP?
Dear Paulo
I guess you need to explicitly set your PATH environment variable and you can do this in more than one way as described here : https://ibmi-oss-docs.readthedocs.io/en/latest/troubleshooting/SETTING_PATH.html
There are some tips on using Python in IBM i here : https://ibmi-oss-docs.readthedocs.io/en/latest/python/README.html The URL above is the first hot link of this Python Usage Note page.
Scott Klement also indicates this may be done from CL. So, please try to identify whichever suits your case.

------------------------------
Right action is better than knowledge; but in order to do what is right, we must know what is right.
-- Charlemagne
Satid Singkorapoom
Original Message:
Sent: Mon January 23, 2023 09:40 AM
From: Paulo Silva
Subject: $PATH on a CLP?
Hi,
I'm running a python scrip from a CLP with QSH with the following command: /QOpenSys/pkgs/bin/python3.9 mypythonscript.py and everything is working perfectly.
However when I do: python3.9 mypythonscript.py I get the message: qsh: 001-0019 Error found searching for command python3.9. No such path or directory.
I'm using the my own user profile.
This is what I see in the QSH shell:
echo $PATH
/QOpenSys/pkgs/bin:/usr/bin:.:/QOpenSys/usr/bin
With the same user btw running call QP2TERM, I get something different:
echo $PATH
/QOpenSys/pkgs/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin
Am I doing anything wrong?
Thanks in advance,
Paulo
------------------------------
Paulo Silva
------------------------------