Relative Python Path to Script - python

Python project looks like this:
setup.py
README
Application
scripts
hello.py
shell_scripts
date.sh
From hello.py I'm executing the command subprocess.call(['../shell_scripts/date.sh']) and receiving the error OSError: [Errno 8] Exec format error.
Note: date.sh is a perfectly valid shell script and is executable. I've also tried os.path.realpath to no avail.
I assume this is due to an invalid path?

Exec format error will come when the shell isn't set at the script. try adding #!/bin/sh at the beginning of the script and execute the python script.

Related

running bash script from python file

I have a bash script which changes the path on my command line,
This one,
#!/usr/bin/env python
cd /mnt/vvc/username/deployment/
I have a python script which i wish to run after the path changes to the desired path,
The script,
#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])
for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)
I get this
File "/home/username/new_file.sh", line 2
cd /mnt/vvc/username/deployment/
^
SyntaxError: invalid syntax
Any suggestions on how can i fix this?thanks in advance
You need to explicitly tell subprocess which shell to run the sh file with. Probably one of the following:
subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])
However, this will not change the python program's working directory as the command is run in a separate context.
You want to do this to change the python program's working directory as it runs:
os.chdir('/mnt/vvc/username/deployment/')
But that's not really great practice. Probably better to just pass the path into os.listdir, and not change working directories:
os.listdir('/mnt/vvc/username/deployment/')

Python can't run absolute script?

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

Opening a python file in terminal. (Ubuntu)

I just installed ubuntu today, and I can't figure out how to run my .py file.
I opened terminal and typed in python test.py and I receive an error saying "python: can't open file 'test.py': [Errno 2] No such file or directory"
If you could please help me out, that would be great.
Thanks
You are using the correct command to open it, but there is no test.py file in your current working directory.
Try using
ls
To verify what files are in fact in your working directory and use
cd path/goes/here
To change directories to where the file is located.
Then try python test.py
python test.py would try to execute the test.py from the current directory.
else try giving the complete path of the test.py
like "python /usr/local/bin/test.py"

failed to run python scripts with jenkins

I am trying to run a Python job. I have created the following folder:
C:\Users\herod\jenkins_ws\workspace\window_testing
and added the script "testing.py" to it.
The script is very simple:
if __name__ == "__main__":
print "hellow world !"
f = open('test_log.txt','w')
f.write("hello\n")
f.close
But I am getting the following error when running it from Jenkins (if I run it from command line it works):
>Building remotely on windows1 (widndows_genereal) in workspace C:\Users\herod\jenkins_ws\workspace\window_testing
[window_testing] $ python C:\windows\TEMP\hudson5234791200924972506.py
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: Cannot run program "python" (in directory "C:\Users\herod\jenkins_ws\workspace\window_testing"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:775)
at hudson.Launcher$ProcStarter.start(Launcher.java:355)
at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1024)
at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:991)
What am I doing wrong ?
Here is what I have tried:
in the configure for the job at the build section I choose "execute python script" and than entered the testing.py file - not working.
I also tried to enter there python testing.py and python.exe testing.py - not working.
Trying to write a python script in the "script" edit text - not working.
If I change the execute type from python to batch file and than it shows that it pass but actually it didn't run the python script.
update2: (after trying Technext solution) I got the next error:
Building remotely on windows1 (widndows_genereal) in workspace C:\Users\herod\jenkins_ws\workspace\window_testing
[window_testing] $ python C:\Users\herod\AppData\Local\Temp\hudson4767788636447260218.py
Traceback (most recent call last):
File "C:\Users\herod\AppData\Local\Temp\hudson4767788636447260218.py", line 1, in <module>
testing.py
NameError: name 'testing' is not defined
Build step 'Execute Python script' marked build as failure
Finished: FAILURE
Whenever running python as a command in batch file, Give the full path of Python executable or you will have to configure the path in the Jenkins environment. Say your python executable is kept in C:\Python27 folder, then execute the following:
C:\Python27\python.exe <full path of python file to execute>
Since the python script runs fine on the command line but has issues when running through Jenkins, it most probably means that the user with which Jenkins is running has issues finding Python executable i.e., python.exe. If it is possible (and feasible) for you to change the Jenkins user, then please change it using the process i described here. Make it run as the same user with which you are running the program successfully on command prompt.
When you install Python in Windows, there is an option to "Add Python to System Path" which you should make sure is selected. You can safely install over your existing Python if you're not sure. This worked for us in Jenkins using the Python plugin.

Python+ubuntu error

Am trying to run the following python program
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
f=open('out.txt')
for a in f:
print regex.findall(a)
print '\n'
when I type the code into the interpreter manually, it works as expected
but when i save it as a file and try to run it , it gives errors.
The command i used to run it is
chmod +x
sudo ./pymod.py
ERROR:
./pymod.py: 2: Syntax error: "(" unexpected
if i dont use sudo, the error i get is
./pymod.py: line 2: syntax error near unexpected token `('
./pymod.py: line 2: `regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")'
am using ubuntu 10.04 with everything on default
it takes about 10-15 seconds for the error to appear
Your file should start with shebang. You should include the path to the python interpreter
#!/usr/bin/env python
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
Check out : http://en.wikipedia.org/wiki/Shebang_(Unix)
This is probably executing as a bash script instead of in Python. Put
#!/usr/bin/env python
at the beginning of your script.
When you set something as executable, you have to specify what you want it to run it with, or Linux will consider it to be a bash script.
Add this as the first line of the file:
#!/usr/bin/python
Or run it like:
python pymod.py
Cheers!
Either use the "shebang". I.e. put
#! /usr/bin/python
as the first line of your script.
Or teach your ubuntu how to treat python scripts without it
as described here: http://www.daniweb.com/code/snippet241988.html

Categories

Resources