This question already has answers here:
Can I use an alias to execute a program from a python script
(5 answers)
Closed 8 years ago.
i hope you guys can help me with this problem because i'm really stuck... I'm trying to execute a program from python and, for some reason, it doesn't work. The script is located at:
path/to/teqc
I've added this line to the .bashrc file:
alias teqc='path/to/teqc'
and, when i run
teqc -tr d input >output
on a terminal it works fine... but, if i run it on a python program, it shows:
sh: teqc: command not found
the code i've been using on python is:
os.system('teqc -tr d input >output')
I tried using
subprocess.Popen('teqc -tr d input >output', shell=True, executable="/bin/bash")
but the only result was to change the error message to
/bin/bash: teqc: command not found
Any help would be really appreciated :)
P.D. I forgot to specify, the operating system is Fedora 21
I would suggest creating a symbolic link to your program .
ln -s /path/to/teqc /usr/bin/teqc
I think the problem is that the environment variable PATH is not the same when you run the command in the code using subprocess.
One solution would be to have a soft link in as suggested in the previous answer
Other thing you can do is to have your code set the environment before you execute your command using subprocess the os module comes has a os.environ dictionary which can be used to append the path using something like this
import os
import subprocess
os.environ['PATH'] += ":/path/to/teqc"
subprocess.Popen(['teqc -tr d input'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
Related
I'm new to stackoverflow and really hope to get answers to my question about crontab.
I'm using MacbookAir in BigSur(don't know if it affects)And my crontab just couldn't run python scripts below.
f = open("test_crontab.txt",'w+')
f.write("Success")
f.close()
But I could run this script with Spyder.
I could edit crontab with crontab -e, and my code in vi crontab was
13 01 * * * /bin/zsh /Users/myusername/Documents/Lynn\'s\ Python\ work/shopee/test.py >>/Users/myusername/Desktop/out.log 2>>/Users/tzulingkuo/Desktop/err.log
But it failed to generate txt file.
The messages in err.log were like below:
/Users/*myusername*/Documents/Lynn's Python work/shopee/test.py:5: missing end of string
/Users/*myusername*/Documents/Lynn's Python work/shopee/test.py:6: unknown username ''
I've searched stackoverflow for two nights and couldn't find the solution. I major in finance and I have no friends studying computer science.
Could anyone help me 🥺
Any help is reaaaaally appreciated!
Since that's a python script, you need to run it in python, not /bin/zsh. zsh is trying to interpret it as a shell script, but the syntax is all wrong, so you get errors.
The simplest way to fix this to replace /bin/zsh in your crontab entry with /usr/bin/python (assuming you're using the built-in Python version 2; if you have installed Python version 3 and want to use that, run which python at the command line to find its path).
But it's generally better to add a shebang line to your python script, and let that control how it's run. Add this as the very first line of the script:
#!/usr/bin/python
(Again, if you want to use Python 3, replace the path part with the path for the Python 3 interpreter.) Then make the script executable with chmod +x /Users/myusername/Documents/Lynn\'s\ Python\ work/shopee/test.py, and just remove the /bin/zsh part from the crontab entry.
BTW, you're probably going to have trouble with paths. By default, cron jobs run with their working directory set to your home directory, so when the script opens test_crontab.txt, that's going to be interpreted as /Users/myusername/test_crontab.txt. If you want it down in the Documents/Lynn's Python work/shopee directory, you'll have to specify that explicitly.
You may also run into trouble with missing environment variables. If you're setting any environment variables in your shell initialization scripts that configure Python, add libraries, etc, those won't be set up in the cron job's environment.
This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.
This question already has answers here:
how to run python script without typing 'python ...'
(5 answers)
Closed 2 years ago.
I have a basic problem where I don't know how to run a Python script from command line in Ubuntu without using python keyword. So, I put a shebang in my Python script so I could run it as nameofthescript from the command line, but I only could do it by using ./nameofthescript. I want to be able to run it by just typing the name of the script in the cmd. I searched and tried everything I could on the web, but none is working. Any help is appreciated. Below is a simple code I wrote to test it.
I already tried chmod +x this file. Also this file is saved with no extension.
#!/usr/bin/python
import sys
def main(argv):
print(argv)
print("Hello")
if __name__ == "__main__":
main(sys.argv[1:])
The problem is with your $PATH variable.
When you go to run a command (without the "./" in front of it) Ubuntu looks in all the folders listed in your $PATH variable. Your can see it by running:
echo $PATH
If Ubuntu doesn't see the command in any of those folders, it will say that it can't be found.
You can solve this problem by altering your $PATH variable in your profile. Go to your home directory and open the ".profile" file (note the period in front) and add the following to the end:
PATH = "/path/to/folder/with/file/:$PATH"
However, if it's a program you could see yourself using a lot in the future and your don't want to clutter up your $PATH, I'd recommend sticking the finished command in your "/usr/local/bin" folder instead. I find that folder gets used as an "odd sock drawer" of programs you create/compile yourself, so I usually end up putting my personal tools in there rather than modifying my $PATH.
That's how it's supposed to work, not only for Python scripts but for any executable. See: Why do you need ./ (dot-slash) before executable or script name to run it in bash?
Please Try this one
On unix systems, Python scripts can be made executable using the following process:
Add this line as the first line in the script:
#!/usr/bin/env python
At the unix command prompt, type the following to make myexe.py executable:
$ chmod +x myexe.py
Move myexe.py into your bin directory, and it will be runnable from anywhere.
$ cp myexe.py /usr/bin
OR
$ cp myexe.py /usr/local/bin
So myexe.py
#!/usr/bin/env python
print("Hello This is executable python script")
Now Go to terminal and type myexe.py
$ myexe.py
Hello This is excutable python script
If you want to run by double-clicking remove .py extention
source link
I have found a way to solve this. I still include the shebang #!/usr/bin/env python3.6 at the top of Python script. Then I would go to cd /etc->sudo nano bash.bashrc, and at the very last line, all I did was add a line (alias nameofscript = "./nameofscript"). From there I restarted my Ubuntu, and was able to run my Python script just by the name of the script. Thank you everyone for the help.
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 4 years ago.
Hi I would like to write a python script so that if i run that script it should open couple of applications and run some commands in console.Can any one guide me through it. Like example scripts and location to place it etc .
P.S: I use Ubuntu as 17 as my OS.
Thankyou
You can use os.open command to run any script within python script it will run as it would in a command line, for example:
import os
os.open("echo 7")
#this will print 7 on the terminal when you run the script
If in case you want to capture output of a script you run and use it in the script then I suggest you use os.popen, for example:
import os
var=os.popen("cat /path/to/file")
print(var)
#this will print the file content
So in short anything that goes in os.open("here") will run as it would in a command line or terminal of your os.
If you want to run applications you will have to sub subprocess:
import subprocess
subprocess.call("spyder")
Alternatively you can use popen as well to open files:
import os
os.popen("spyder or subl")
os.open will not work. In regards to your specific request use the following code:
import os
os.popen("cd /home/mypc/path ; subl")
You can use the os library to execute system commands
import os
os.system("your command")
you can add your wanted application to the system path to be able to execute it using system commands
This question already has answers here:
Find full path of the Python interpreter?
(3 answers)
Closed 6 years ago.
I would like to output, in my script, the full path of the Python interpreter running it:
#!/usr/bin/env python
print("{}".format(full_path_of_interpreter_running_this_script)
The script is in the PATH and run as:
script.py
Can I do that? How?
Note: Doing which python or type python in bash does not help me, because I am using pyenv, and pyenv is doing shims magic.
Note: More than identifying the Python executable, I am interested in identifying the virtualenv that is being used, and I thought knowing the full path to the interpreter will help me in this.
This gives the full path to the command that was used to run the script:
import sys
print(sys.executable)