Python is seeing some problem with how I am opening a file with the code below
if __name__ == "__main__":
fileName = sys.argv[1]
with open(fileName, 'r') as f:
for line in f:
print line
It is producing the error
./search.py: line 3: syntax error near unexpected token `('
./search.py: line 3: ` with open(fileName, 'r') as f:'
Am I missing an import? What could be the cause of this?
EDIT: OS - CentOS, Python version 2.6.6
Not sure how I installed, I am running an image from a .edu openstack site. Not sure of the distribution, binaries, ...
You must add import sys in order to use sys.argv. Check this out.
I have tried this:
chmod u+x yourfile.py
./yourfile.py
and it gives me:
./jd.py: line 4: syntax error near unexpected token `('
./jd.py: line 4: ` with open(fileName, 'r') as f:'
If you are doing ./search.py file then add at the beginnig of your file #!/usr/bin/env python. Otherwise, use python file.py input
The problem is that you aren't running your program with Python at all! When you do ./script (assuming that script is a text script, and not a binary program), the system will look for a line at the top of the file beginning with the sequence #!. If it finds that line, the rest of the line will be used as the interpreter of that script: the program which runs the script. If it doesn't find that line, the system defaults to /bin/sh.
So, basically, by omitting the magic line #!/usr/bin/python at the top of your script, the system will run your Python script using sh, which will produce all sorts of incorrect results.
The solution, then, is to add the line #!/usr/bin/python (or an equivalent line, like #!/usr/bin/env python) to the top of your Python script so that your system will run it using Python. Alternately, you can also always run your program using python search.py, instead of using ./search.py.
(Note that, on Linux, filename extensions like .py mean almost nothing to the system. Thus, even though it ends with .py, Linux will just execute it as if you wrote /bin/sh search.py).
Either:
the first line of search.py should be a #! comment specifying the path to locate the python executable, usually [#!/usr/bin/env python](Why do people write #!/usr/bin/env python on the first line of a Python script?
on-the-first-line-of-a-python-script). Usually this is #!/usr/env/bin python . Don't use a hardpath e.g. #/opt/local/bin/python2.7
or else you can invoke as python yourfile.py <yourargs> ...
PREVIOUS: If import sys fails, post more of your file please.
Maybe your install is messed up.
Can you import anything else successfully, e.g. import re?
What are your platform, OS and Python version? How did you install? source? binaries? distribution? which ones, from where?
Related
On Terminal,
>> which python
/Users/Chois/.pyenv/shims/python
aa.py
# !/Users/Chois/.pyenv/shims python
print("a")
On Terminal,
chmod 755 aa.py
And execute it,
./aa.py
It occured errors
./aa.py: line 3: syntax error near unexpected token `"a"'
./aa.py: line 3: `print("a")'
What's wrong with it?
Rather than using full path for the python binary, your shebang line could use the env instruction. Then, your shebang line will end up being something like this:
#!/usr/bin/env python
The shebang line is blatantly wrong... You shouldn't have a space between the hash and the bang:
#!/Users/Chois/.pyenv/shims/python
There's also a missing slash that I filled it for you.
I need some advice with a Python script. I'm still new and learned it by myself. I found the script on Google. After I retype it, it doesn't print the result in the console. How can the result of the script be shown in the console? Details as below:
C:\Python27>test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d
C:\Python27>
After I press enter, nothing happens. Should the result be shown or not?
Here's the code that I retyped:
#!/usr/bin/python
#coding: ascii
import requests
import sys
import re
url = 'http://hashtoolkit.com/reverse-hash?hash='
try:
hash = sys.argv[1]
except:
print ("usage: python "+sys.argv[0]+" hash")
sys.exit()
http = request.get(url+hash)
content = http.content
cracked = re.findall("<span title=\*decrypted (md5|sha1|sha384|sha512) hash\*>(.*)</span>", content) # expression regular
print ("\n\tAlgoritmo: "+cracked[0][0])
print ("\tPassword Cracked: "+cracked[0][1])
The first line in your script is called a Shebang line.
A Shebang line tells the script to run the Python interpreter from that location.
The shebang line you provided is a Linux system path, but it looks from the path you are executing Python from, that you are running on Windows.
You can do one of two things here to fix that:
Remove the Shebange Line.
Remove the first line from your script.
Execute the script using python test1.py COMMAND_LINE_ARGUMENTS
Modify Your Shebang line.
Change the first line of your script from !/usr/bin/python to
#!python (This is assuming that python is in your systems PATH variable.)`
Execute the script using test1.py COMMAND_LINE_ARGUMENTS
Also, you are trying to import the requests module that is not installed in the standard library.
If you haven't installed this yet, you can do so by going to your Python install directory and go to the scripts folder.
Hold shift and right click and go Open command window here
Type pip install requests and hit enter.
After that you should be good to go, execute the script by navigating to it and type test.py COMMAND_LINE_ARGUMENT
If a Python script doesn't have the shebang line:
python test.py COMMAND_LINE_ARGUMENT
you need to run your script using python. try:
C:\Python27>python test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d
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.
I am using the input function from fileinput module to accept script via pipes or input file Here is the minimum script:
finput.py
import fileinput
with fileinput.input() as f:
for line in f:
print(line)
After making this script executable, I run ls | ./finput.py and get unexpected error message
./finput.py: line 1: import: command not found
./finput.py: line 3: syntax error near unexpected token `('
./finput.py: line 3: `with fileinput.input() as f:'
The only fix I found is when I add #!/usr/bin/env/python3 before the import statement.
But this issue seems to be related only to the fileinput module. Since the following script worked well without a shebang:
fruit.py
import random
fruits = ["mango", "ananas", "apple"]
print(random.choice(fruits))
Now what am I missing? Why can't the import command be found since the shebang is not required in finput.py?
Your need to tell your OS that this is a Python program, otherwise, it's interpreted as a shell script (where the import command cannot be found).
Like you identified, this is done by using a shebang line:
#!/usr/bin/env python3
This is only needed if you are going to run your script like this: ./script.py, which tells your OS "run this executable". Doing so requires that your OS identify how it's supposed to run the program, and it relies on the shebang line for that (among other things).
However if you run python script.py (which I'm guessing you did for fruit.py), then Python does not ask your OS whether that is a Python program or not, so the shebang line doesn't matter.
I have just started learning Hadoop. I tried to run a simple mapreduce job on it, but before that I tried to check it locally. But its returning error. Kindly suggest any solution to it. I am using Ubuntu 12.04 LTS.
SO the code is written in gedit, and is ad follows.
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print '%s\t%s' %(word,1)
Then I write the below command in terminal to check if mapper is working fine
maitreyee#bharti-desktop:~$ echo "foo faa" | /home/maitreyee/Documents/mapper.py
and the terminal returns the following error:
/home/maitreyee/Documents/mapper.py: line 1: import: command not found
/home/maitreyee/Documents/mapper.py: line 5: syntax error near unexpected token `line'
/home/maitreyee/Documents/mapper.py: line 5: `line = line.strip()'
You are missing the shebang line at the top of your script. Add something like this (whichever python makes sense for your machine):
#!/usr/bin/python
Here I use the system python under /usr/bin/python
The shebang line is needed because you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH.
If you want more to know about writing map reduce code in python, you can follow this
tutorial!