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...)
Related
I'm on a MacBook Pro 16" (with MacOS Catalina). I want to run Python scripts directly via Spotlight search. I don't want to have to open any IDE or the Terminal. I have seen instructions that tell me to:
Write and save my Python code e.g.: print("Hello World"), saved as hello.py in home folder Users/Gory
Create a text file using TextEdit and save it with .command file extension (e.g.: samplescript.command). The file should contain the following
#!/usr/bin/env bash
python3 /Users/Gory/hello.py
Make the shell script (samplescript.command) created above executable by running in the Terminal:
chmod u+x samplescript.command
After following the above steps, I searched for samplescript.command via Spotlight and pressed enter. I expected to see "Hello World" printed on a terminal window. Instead I get the following message:
MacBook-Pro:~ Gory$ /Users/Gory/samplescript.command ; exit;
/Users/Gory/samplescript.command: line 1: {rtf1ansiansicpg1252cocoartf2511: command not found
/Users/Gory/samplescript.command: line 2: syntax error near unexpected token `}'
/Users/Gory/samplescript.command: line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
What is wrong?
Your problem
Your problem is caused by your second step:
Create a text file using TextEdit and save it with .command file extension (e.g.: samplescript.command). The file should contain the following
By default, TextEdit uses the Rich Text Format.
Consequence: Your samplescript.command file does not contain what you expect
#!/usr/bin/env bash
python3 /Users/Gory/hello.py
but actually
{\rtf1\ansi\ansicpg1252\cocoartf2513
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 #!/usr/bin/env bash\
python3 /Users/Gory/hello.py}
Such content is not a valid command, and then results in the observed error when executed:
line 1: {rtf1ansiansicpg1252cocoartf2513: command not found
line 2: syntax error near unexpected token `}'
line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
How to fix it?
If you want to use TextEdit then change the format from Rich Text to Plain Text before typing in anything.
Create a new document
Format menu ➜ Make Plain Text
Insert your content, save
Better fix:
Do not use TextEdit or any other kind of text processor application to write code (scripts), so that these kind of problems are avoided from the start.
In any case, whatever editor you use double-check that the (script) file content is actually the content you expect.
I am a learner, and had a similar issue.
Mine worked with an extra space in the shebang line, and I ran chmod to the full file path.
"#! /usr/bin/env bash"
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")
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.
I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.
When working in Bash on Linux (Ubuntu 12.04 LTS), text output is truncated at times. I'll explain what I mean.
Suppose, you issue the "dpkg -l" command. dpkg then writes to its standard output a long list of installed packages, the lines of text are not truncated and everything's OK. Now, you try to dump the output of the command to a file - "dpkg -l > dump". When looking at the file contents, you'll see that some of the lines (those that are quite long) have been truncated. What logic is responsible for this behavior?
Also, I conducted a small experiment. When working with the Python interpreter interactively, I type the following:
from os import popen2
child_input, child_output = popen2("dpkg -l", "t") # running the command "dpkg -l" in a subprocess so that I can then obtain what it writes to its stdout via a pipe
dump_file = open("dump", 'w') # create a file to write to
for string in child_output:
dump_file.write(string) # writing the child's output to the dump file, one line at a time
dump_file.close()
When typed interactively, the code works fine - no truncation occurs. Once I place that code in a script and run it, some of the long lines get truncated. Why does this happen?
You can try using COLUMNS variable before executing commands in bash;
feel the difference:
COLUMNS=80 dpkg -l
and
COLUMNS=300 dpkg -l