Running python programmes for the command line - python

I am trying to run a python programme called helloWorld.py from the command line on MacOS.
I am typing into bash:
Ryans-MacBook-Air:~ ryanunderwood$ python3 helloWorld.py
But I am getting the following:
/Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'helloWorld.py': [Errno 2] No such file or directory
There is definitely a file with this name in the directory. Why is this programme not running?

Try ./helloworld.py or python3. /hello World.py
And make sure you have the right permissions

Related

No such file or directory when running a python script in WSL

I'm trying to run a python script in WSL that runs Ubuntu.
In WSL, the following command does not work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe /mnt/c/Users/admin/repo/script.py
C:\Users\admin\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file '/mnt/c/Users/admin/repo/script.py': [Errno 2] No such file or directory
However, the following individual commands work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe repo/script.py
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe
admin:/mnt/c/Users/admin$ cat /mnt/c/Users/admin/repo/script.py
Why am I seeing this strange behavior? How do I run a python script using absolute path?

No such file or directory on Mac terminal for python

hello.py is my first python program. It is saved on my desktop.
In the terminal I write in front of
user#AA-MacBook-Air ~ % python3 hello.py
The error is
can't open file 'hello.py': [Errno 2] No such file or directory
Kindly help me understand the problem and solve it.
In the terminal you are currently in the directory ~. This signifies the folder /Users/<username>. Your script is on your desktop.
Type cd Desktop to change to /Users/<username>/Desktop and then run python3 hello.py.
you first need to change destination with cd
The error message, No such file or directory pretty much gives the explanation. Check if the file hello.py is present in the correct working directory. This can done graphically or using the ls command. If it is not present, copy the file the to the directory or navigate to the location of the file hello.py in terminal using cd.

Executable .py file with shebang path to which python gives error, command not found

I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):
which python
<user>/bin/python/Python-3.6.1/python
I have an executable (chmod 777) sample.py file with this line at the top of the file:
#!<user>/bin/python/Python-3.6.1/python
I can execute the file like this:
python sample.py
But when I run it by itself I get an error:
/full/path/sample.py
/full/path/sample.py: Command not found
I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?
EDIT:
I tried putting this on top of the file:
#!/usr/bin/env python
Now, I get this error:
: No such file or directory
I tried this to make sure my env is correct
which env
/usr/bin/env
EDIT2:
Yes, I can run the script fine using the shebang command like this:
<user>/bin/python/Python-3.6.1/python /full/path/sample.py
Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).
Try using #!/usr/bin/env python as described in this post. Let the OS do the work.

Auto Starting a python script on boot (RPi)

I have a python script on my RPi that needs to run on boot
I added it to rc.local, and it used to work fine
A few days ago, I added a functionality to the program, and it now uses open() to read a txt file
Now every time I restart the Pi, python gives me and error:
File "home/pi/client.py", line 13, in <module>
stats=open('stats.txt')
IOError: [Errno 2] No such file or directory: 'stats.txt'
When I manually launch the script with:
sudo python client.py
it works fine with no problems.
Any suggestions?
Your rc.local probably does not start your script in the correct directory. So you should either:
use something like cd my/dir && python /path/to/home/pi/client.py
call os.chdir("/path/to/some_dir") in your script
use an absolute path when opening the file: stats = open('/path/to/stats.txt')

How to run wlst script by .py file

I'm trying to run wlst script form .py file but it can not be done
Content of .py file :
connect('weblogic','weblogic','t3://localhost:8001')
sca_undeployComposite('http://localhost:8001','Hello','1.0','user='weblogic',partition='myPartition')
sca_deletePartition('myPartition')
sca_createPartition('myPartition')
sca_deployComposite('http://localhost:8001','C:\WLST\Test\Application.zip',user='weblogic',configplan='myPlan.xml', partition='myPartition')
exit()
when i run cmd file to execute script, Only connect() method is execute success. any command bellow it can not be execute. And error message appear: Problem invoking WLST - Traceback (innermost last): File "c:\WLS\script\filname.py", line 2, in ?
Name Error: sca_undeployComposite
Please help me to resolve it. Thanks !
The commands after the connect() line which are not regular WLST commands. They requires sca related libraries into CLASSPATH. if you look into your wlst.cmd or .sh file that is actually calling the environment setup file that could be setWLSEnv.sh/.cmd. If you run that from where you are having the this python script. That script will work, it is simple java CLASSPATH funda nothing else!
Probably you might be running wlst.cmd after navigating to the common bin folder like
cd /oracle/fmwhome/Oracle_SOA1/common/bin/.
instead you can run in your script like this
C:\WLS\script\>/oracle/fmwhome/Oracle_SOA1/common/bin/wlst.cmd filename.py
or
C:\WLS\script\>/oracle/fmwhome/Oracle_SOA1/common/bin/setWLSEnv.cmd
C:\WLS\script\>java weblogic.WLST filename.py
You can also refer for more sca related scripting: WLSTByExamples

Categories