简体   繁体   中英

copy then paste produces garbage in python terminal mac

I am on Mac OS X (10.14.3) using a text editor (Sublime Text 3) and, in a different window, a Terminal window running python (3.7) interpreter.

The code I copying from Sublime (using cmd+c) is

def count_numbers():
    for i in range(10):
        if i % 3 == 0 : 
            print (i)

count_numbers()

But the output I get when I copy paste into the terminal (I included the command to open the python interpreter) is:

$ python
Python 3.7.2 (default, Dec 30 2018, 08:55:50) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

[here is where I press cmd + v ]

>>> def count_numbers():
...         return a * x + b
... z=[invcdf(random()) for i in range(10)]
  File "<stdin>", line 3
    z=[invcdf(random()) for i in range(10)]
    ^
SyntaxError: invalid syntax
>>>     if t2 is None:
  File "<stdin>", line 1
    if t2 is None:
    ^
IndentationError: unexpected indent
>>>         # print (i)
... 
>>> count_numbers()

This other mixed-up code is from some python I was writing yesterday. Since then I have used copy/paste countless times, so I don't understand why my recent copy (cmd + v) is channeling yesterday's work complete with apparent indentation errors, bits of today's code, and so on.

Typing command+shift+v is no better: just different garbage.

What on earth is going on?

Mac OS X : 10.14.3 python installed by MacPorts
Tkinter not installed: I don't like IDLE.

The configuration I use is similar to yours, command line + Xcode, I tried copy and pasting your code from the IDE into the command line with everything working as expected. So I believe that the problem is not within the command line. I would check the clipboard, (Finder -> Edit -> Show Clipboard) to see if the code is copied correctly, it should appear exactly as it looks in the IDE. If not then I would suggest commenting out unneeded code and running the file directly from the command line.

The .py file can be run directly from the command line without launching the python interpreter using the command python3 path/to/file , for which you can drag the file directly into terminal and it will automatically enter its path. This can be further simplified by using the interpreter shebang #! in the .py file itself. The path of the interpreter can be found by importing system import sys and print(sys.executable) . This gives the path to the interpreter, then add the shebang to the path and put it in the top of the .py file, for me it looks like this: #!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 , but since you installed it from MacPort it might be different. The shebang allows the script to be run in the command line simply by entering the path to the file. ie drag the file into terminal, hit enter, and it runs. You can also use the terminal shortcut of shift + up arrow and then hit enter to run the last command again, saving you the time to keep dragging the file into terminal.

OK,

finally worked out that the pboard process was corrupted.
best way to fix is to kill the pboard process in activity monitor or terminal

using

http://tips.applenws.com/copy-paste-not-working-on-mac-heres-how-to-fix-a-stuck-clipboard/2018/tips-tricks

All working well now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM