Why my python script is not printing anything - python

I have a problem..whenever I am writing any Python script say like this
#!/usr/local/bin/python
print "hello"
Then using
chmod +x a.py
And then write ./a.py then it is not printing anything in the terminal
Moreover whenever I write any comment below the shabang line, it gives me an error saying
#: bad interpreter : No such file or directory
but when I run the script like this python a.py it works as usual..
Can someone tell me what's wrong and how to fix this..

This is almost certainly because your line ending is a carriage-return/line feed combination - which Windows-style editors will create. Unix regards the LF as the end of the line, so it's looking for an executable called "python\r". When you run it with an explicit call to the interpreter the shebang line is just treated as a comment.

Under linux text replacement fix:
sed -i 's/^ M//g' filename
(note that ^ M is written in linux, press ^ M is carriage return and line feed, the input method is to hold down CTRL + v, release v, press m)
This solve bad endline for shell in first line if error message looks like this:
/usr/bin/python3 ^M: bad interpreter: No such file or directory

Related

Python: Can not run python file without `python` command on Mac?

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.

How can I use files/directories in python on Linux that start with .?

I'm a COMPLETE beginner to python, I'm making my first python script that really does anything. I'm assigning a directory to a variable so I can use it to move a file, but the directory includes a folder starting with . and python says it's invalid syntax. So how can I get python to ignore the .?
EDIT: Here's the code
#!/usr/bin/env python
import os
Optifine = find /home/Sol33t303/.minecraft -name
OptiFine_1.10.2_HD_U_E3.jar
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
You are mixing two very different things.
Are you writing Python or Bash, cause this is totally Bash:
Optifine = find /home/Sol33t303/.minecraft -name
You can't just run Bash commands inside a Python script!
If, for example you want to run a shell command inside your script and get its output you can use:
Optifine = subprocess.check_output(['find', '/home/Sol33t303/.minecraft', '-name'])
And then you split the output by line, and foreach line (file found), move it to the desired destination:
for line in Optifine.splitlines():
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")

What happened to my Ubuntu Desktop 16 today - cannot execute Python file as usual

In brief
I can't run a simple Python file with +x permission set and shebang line.
In Details
Let's take a simple Python code in myApp.py file at some $CODE_HOME folder
#!/usr/bin/python
print 122
When cd $CODE_HOME and running this file from console
. ./myApp.py
I got error as
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "122333"
Though running by python myApp.py will get thing work.
The question
What's wrong is that? How to fix it?
. myApp.py is an instruction to Bash to source the passed file, ie execute it within the current process.
To execute a script or other file, you need to reference it by path:
./myApp.py (or just python myApp.py)
i.e. omitting the starting '.' in your call
To answer your question as is, . is the source command, which just runs each of the commands in the argument script in the context of the calling terminal. In your case, this doesn't do anything for the first line, then tries to call print, as you can see in
at /usr/bin/print line 528
Use ./myApp.py instead.

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?

How can I send line number from Python traceback into vim?

I can parse out the paths to the files of a Python traceback, and I can then send those on to Vim using -p on the command line, so that they are opened one file per tab. So I end up with a command like, for example
vim -p main.py module.py another.py
That opens each file in a new tab, but I would like them opened in a new tab, at the correct line number. So I have tried variations like
vim -p main.py +10 module.py +20 another.py +30
But I cannot seem to get Vim to respect the line numbers I send in on command line - it always just takes the last line number and applies it to first tab. So the example left me in main.py at line 30. Trying variations like
vim -p main.py+10 module.py+20 another.py+30
vim -p main.py\ +10 "module.py +20" another.py#30
all just ended up with bad filenames.
Answers at the level of Python, or Bash command line, or within Vim script, or Vim-Python would all be welcome. Or, indeed, entirely different approaches
(The tracebacks could come from anywhere, and are not necessarily controllable by me. The one that started me off today was just a set of lines in a log from a server.)
Try vim plugin: file_line:
vim -p new main.py:10 module.py:20 another.py:30
Known Issue: the first filename should not have a lineno. (I'm trying to figure out WHY...)

Categories