Why can't the import command be found? - python

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.

Related

Python script doesn't print output in command prompt

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

THEANO_FLAGS: Command not found

My settings of .theanorc file is device = gpu0, but I want to know if I can run one program with gpu0, and run another with gpu1, I tried THEANO_FLAGS='device=gpu1' python myscript.py but it raised THEANO_FLAGS: Command not found.
just add these 2 lines in the script:
import os
os.environ["THEANO_FLAGS"] = "device=gpu1"
THEANO_FLAGS='device=gpu1' python myscript.py is working for me.
Are you sure you have no space character after THEANO_FLAGS, like this: THEANO_FLAGS ='device=gpu1' python myscript.py.
This would raise the THEANO_FLAGS: Command not found

Error after running mapper.py locally in terminal

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!

Python encountering unexpected ')` in very short program

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?

Why does importing subprocess change my output?

I noticed the following using Python 2.5.2 (does not occur using 2.7):
#!/usr/bin/python
import sys
for line in sys.stdin:
print line,
Output:
$ echo -e "one\ntwo\nthree" | python test.py
$ one
$ two
$ three
as expected. However, if I import subprocess to the this script:
#!/usr/bin/python
import sys
import subprocess
for line in sys.stdin:
print line,
Output:
$ echo -e "one\ntwo\nthree" | python test.py
$ two
$ three
What happened to the first line of output?
Update:
I think I may have discovered the root of the problem. I had a file named time.py in my cwd. A time.pyc is being created every time I run the script with subprocess imported, suggesting that ./time.py is also being imported. The script runs normally if I delete the .pyc and time.py files; however, there is still the question of why a subprocess import would cause ./time.py to be imported as well?
I have narrowed it down even further to the exact line in time.py that causes the strange behaviour. I have stripped down the working dir and file content to just that which affects the output:
test.py
#!/usr/bin/python
import sys
import subprocess
for line in sys.stdin:
print line,
time.py
#!/usr/bin/python
import sys
for line in sys.stdin:
hour = re.search(r'\b([0-9]{2}):', line).group(1)
Running test.py with any kind of input results in the first line of output being omitted and time.pyc being created.
Sounds like your local time.py will be imported instead of the global time module. You might want to rename it, or at least start checking if it was run as a script or imported as a module.
This will prove it for you if you want to test it.
#!/usr/bin/python
import sys
# Test that script was run directly
if __name__=='__main__':
for line in sys.stdin:
hour = re.search(r'\b([0-9]{2}):', line).group(1)
else:
print 'Imported local time.py instead of global time module!'
sys.exit(1)
print line, before 2.7 did not put out a newline so it overwrote the first line. Lose the comma and the result will be the same.

Categories