How can I execute commands through python in terminal MAC? - python

When I manually run this command in Terminal, it executes, but through Python it gives the error that the directory is not available in Python packages.
I am using the following command
source ~/trytry/shell.sh

This is my test shell file:
#!/bin/sh
echo hello
when I executed " source ~/test.sh ", it will print hello at console.
This is my python code:
>>> import commands
>>> commands.getstatusoutput("source ~/test.sh")
(0, 'hello')
It works without any problem. So, would you please show your code?

What it looks like to me is that you have a shell script, and not a python file which would have the .py extension instead of .sh. The error may have to do with the fact that it isn't a python file you're trying to run.

Related

Why This Program is not Running [duplicate]

I'm trying to learn Python and am trying to execute a python file in terminal. I'm using 2.7.3 python version on my OS X. I've changed the directory in terminal to where the file is located, but I'm getting an error in terminal:
>>> python ex1.py
File "<stdin>", line 1
python ex1.py
^
SyntaxError: invalid syntax
The ex1.py file contains:
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
Any ideas on how to fix this? Thx a bunch.
>>> python ex1.py
You are trying to run your script from within a python interpreter. You don't want to do that.
Instead, just run that command in a terminal, not in the interpreter
$ python ex1.py
If you are still in the interpreter, you can press ctrl+d to leave it and return to the 'normal' terminal
Exit python interpreter by typing exit() or press Crtl+Z
You will see the terminal now.
Type python file_name.py to run the code.
Happy coding!!!
Try type
exit()
return; then
python ex1.py
return

Execute commands from python prompt using os.system

I am trying to execute a few commands using python shell to import a module ABC. For that I have created a python file test.py having the following lines:
import os
os.system('python')
os.system('import ABC')
Now I am trying to execute that file as follows - python test.py. Here, it runs till the 2nd line i.e. os.system('python') properly. Then when it enters into the python prompt and there it remains stuck as it is not command prompt any more but python prompt. So how to execute the next command of importing the module ABC on the python prompt?
Command line and environment (Python Documentation)
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
In your terminal, when you run python followed by the name of a script (test.py), you are executing the contents of the script. Within that script, when you call 'python' using os.system('python'), you are starting a new interactive session, similar to if you were to just call 'python' from your terminal.
import os
os.system('python -c "import ABC"')
You can use the -c argument with python.
For example,
import os
os.system('python -c "import ABC"')
It sounds like what you need is to put the commands you require in a Python script, e.g. my_imports.py:
import ABC
import my_module
And run it from interactive Python with something like:
exec(open('my_imports.py').read())
That will execute the code in the script in the current context, making the imports available to you on the Python CLI.

How can I get the result from R program with Python?

I have the R program which result I want to get with Python. First, I have tried this:
import subprocess
subprocess.call ("C:\py\test.R", shell=True)
But this program returns 1, and it is not that I`d like to get. Also I tried to install rpy2, but I had error with its installing.
Then I tried this:
import subprocess
command = ("C:\R400\bin\Rscript.exe " "--vanilla C:\py\test.R")
subprocess.Popen(command)
variable_name = subprocess.check_output(command)
print(variable_name)
But this code (Jupyter Notebook and IDLE Python 3.8 64 bit on Windows 10) gets an error:
FileNotFoundError
What could be the reason? How can I fix it? How can I run a R program with Python, but without using rpy2?
Also I tried with cmd. I create a file run.py in the folder with test.R:
import subprocess
subprocess.check_call(['Rscript', 'test.R'], shell=False)
But where was nothing when I tried python3 run.py with cmd.
Also I failed with python3 run.py > testout.txt when I tried to write the result down in the special textual file.

I do not understand this syntax error

I am doing the Python tutorial byte of python 120-1 pdf
Here are the instructions:
Start your choice of editor, enter the following program and save it
as helloworld.py
Example 3.2. Using a Source File
#!/usr/bin/python
# Filename : helloworld.py
print 'Hello World'
(Source file: code/helloworld.py) Run this program by opening a shell
(Linux terminal or DOS prompt) and entering the command python
helloworld.py. If you are using IDLE, use the menu Edit -> Run Script
or the keyboard shortcut Ctrl-F5. The output is as shown below.
Output
$ python helloworld.py
Hello World
I entered the program into text wrangler and saved it as helloworld.py
I then opened my terminal and entered python helloworld.py.
I received syntax error: invalid syntax
I then tried helloworld.py and also received syntax error: invalid syntax
Can anyone tell me where I went wrong?
You're using 2.x documentation, but are running 3.x. Either downgrade Python, or find more recent material.
#!/usr/bin/python
# Filename : helloworld.py
print('Hello World')

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