this is my first time trying to get a Python script to work and not having much luck.
I have a file called 'alpha_script.py' with the following in:
#!/usr/bin/env python
print "the script is running now"
that is in the /bin folder of a project.
Then I run this in terminal
chmod +x alpha_script.py
nothing happens after I hit enter though, not sure if thats correct or not.
I can run the file just fine with a normal Python command but when I enter this in the terminal
./alpha_script.py
it returns this error message
./alpha_script.py: line 4: print: command not found
what am I doing wrong here?
The issue is in your shebang line -
#!/usr/bin/env python
^ Notice the extra space.
The extra space at the starting of the line is causing the issue (That shebang line is not getting picked up correctly) , remove that space and it should work.
There are roughly four things to check here.
Is the #!/usr/bin/env python literally at the very beginning of your file? Are there any "hidden" or whitespace characters preceding the the #!?
Do you actually have the /usr/bin/env utility present and executable? (You almost certainly do, it's standard in Linux and other forms of UNIX. But check anyway).
Do you have 'python' on your path, under exactly that name?
What version of Python do you have installed as python?
I suspect that you just have some space or something in front of your "shebang" (#!) marker. This will cause some shells to treat the entire file as a shell script and would give the error message that you're getting.
However, the broader lesson to learn is that every on of these things I've enumerated is imported to getting any sort of script running for any scripting language. You must have a valid "shebang" line (meaning that #! must be the very first characters of the script) ... the path after the #! must be valid ... if you're using env then the interpreter named after the space must be valid and on the path ... and lastly that Python changed from versions 2.x (and earlier) to the new 3.x (print is no longer a statement ... it's now a function and must be rendered as print() with the parentheses around its arguments).
Related
I am very new to python and I am trying to execute a python file called HelloWorld.Py. The code within the file editor is
#! Python3
print('Hello world!')
I am trying to do this from my windows command line. The correct path from what I can tell in my documents application is C:\Users\china\PythonFiles\HelloWorld.py.
As I am following a Udemy course, it is recommended that you type py.exe C:/Users/china/PythonFiles/HelloWorld.py (filled in with my example).
At first, I returned a syntax error for the use of py.exe. Reading online made me confused about what to put in front of C:, and after removing py.exe, and just typing C:/Users/china/PythonFiles/HelloWorld.py returns the error 'Unable to create process using 'Python3 "C:\Users\china\PythonFiles\HelloWorld.py" '
What am I doing wrong? I have researched online for a few hours and I am still no closer to figuring it out. Please help!
Remove the comment, or change it to #!python3. py.exe doesn't like the capitalization.
The comment isn't needed unless you have both Python 2 and Python 3 installed, and it hints to py.exe which Python to use, but requires lowercase python followed by the major and optionally minor version, e.g #!python3.6 or #!python3. If the comment is missing, the latest version of Python installed wiil be used.
I'm running a script that involves a lot of foreign characters, and the classic cmd line seems to choke on them, which might be related to my Windows setup. It runs fine in IDLE and other IDE's. Is there anyway that I can launch my script automatically from another place (e.g. IDLE) without first having to open another program?
You can just make a shortcut of the form:
"C:\Python27\python.exe" "C:\location\of\your\script.py"
Obviously substitute your version of Python. If you include arguments that also point to a file that contains your foreign characters, you can have it load that as well.
To have the command windows stay open, use one of the following at the end of your script:
# Python 2
raw_input()
# Python 3
input()
Unable to get shebang line working in Ubuntu for python script. I only get a command not found error each time.
test.py
#!/usr/bin/env python
print ('!')
Ran
:which python
/usr/bin/python
Played around with different locations for python in the shebang but no luck including what was provided by which python. Any tips on how to troubleshoot this?
Thanks
If you are trying to run the command as
$ test.py
the error may not have anything to do with the shebang. Rather, the directory that test.py resides in is not in your PATH. Try
$ ./test.py
to bypass PATH lookup.
(This is in addition to making sure that the script itself is executable.)
On the python docs page it says:
To easily use Python scripts on Unix, you need to make them
executable, e.g. with
$ chmod +x script and put an appropriate Shebang line at the top of
the script. A good choice is usually
#!/usr/bin/env python
which searches for the Python interpreter in the whole PATH. However,
some Unices may not have the env command, so you may need to hardcode
/usr/bin/python as the interpreter path.
I don't know if this applies for you or not.
Apart from executing the script with a preceding dot or making it executable, there might be another issue:
If you try to use a script written with a windows editor, it may contain windows line endings. Removing these can make the shebang work again.
To remove such line endings, refer to How to convert Windows end of line in Unix end of line (CR/LF to LF) for instance.
See also my general remarks on failed shebang evaluations at my other answer.
Make sure that the "FIRST LINE" is the shebang.
Do not give any newline character in the beginning of the file.
"No newline character in beginning"
This may be due to a kernel misconfiguration. Take a look at your kernel's config options, and check if CONFIG_BINFMT_SCRIPT is set:
zcat /proc/config.gz | grep CONFIG_BINFMT_SCRIPT
If the output of this command is anything besides CONFIG_BINFMT_SCRIPT=y, this means that your kernel will not allow you to use shebangs. You will need to get a new kernel or recompile your current kernel with CONFIG_BINFMT_SCRIPT=y.
You can also check your line ending in gitbash/pycharm terminal by typing,
cat .gitattributes
this will list the setup, for linux setup paste this
*.sh text eol=lf
if this doesnt work, then better create a new branch and delete the exisiting file and create newfile.
This worked for me
Python code:
"<stdin>"
#!/usr/bin/env
print "Hello world!"
print "How are you?"
The above is some code that I applied onto a script and it works when run in TextWrangler, but when I put it in Terminal, it fails.
Why is this happening? Does it have to do with the way I open the file?
/usr/bin/env is not the correct path to Python. Most likely the shebang line should read:
#!/usr/bin/env python
I am pretty sure it needs to be the first line, so delete the "<stdin>" line as well (that's ignored by Python anyway).
Also make sure you have set the execute permission on the script: chmod +x /path/to/script.py
The line that starts with #! is called the shebang line in Unix. By definition, two things are wrong with your shebang:
The sheebang MUST be the initial line in the script -- you have it on the second line.
If Python is the target interpreter, you either need the absolute path to Python (something like #!/usr/bin/Python OR an argument to env to execute the configured Python -- something like #!/usr/bin/env Python note the argument 'Python' to the utility env
I am trying to call a perl script from my python program with the following line:
subprocess.call(r'/path/to/compute_lexrank.pl /path/to/11sent',shell=True)
when I run the same perl script from the shell (just typing /path/to/compute_lexrank.pl /path/to/11sent) it works fine as expected, but when I run it from the python program, the perl script is executed, but gives a mysterious error:
Math::MatrixReal::new(): number of rows must be integer > 0 at /Users/filippo/Downloads/clairlib-core-1.08/lib//Clair/Network.pm line 1628
now because I havent wrote the perl script myself I dont know how to fix this, but why the same script behaves differently when I run it from the shell or from subprocess.call?
I am using MacOsX, python 2.6 (but I have tried also with 2.5, same stuff) and perl 5.10.
Anyone can help?
In Perl, there's a good rule: if possible, use list forms of popen and system. Python seems to have those, too. I wonder what happens if you try this out:
helper = "/path/to/compute_lexrank.pl"
helper_input = "/path/to/11sent"
subprocess.call([helper, helper_input])
You may need to pass the working directory to the subprocess.call
subprocess.call(r'/path/to/compute_lexrank.pl /path/to/11sent',shell=True,cwd="/path/to")
If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.