简体   繁体   English

复制然后粘贴在python终端mac中产生垃圾

[英]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. 我在Mac OS X(10.14.3)上使用文本编辑器(Sublime Text 3),在另一个窗口中是运行python(3.7)解释器的Terminal窗口。

The code I copying from Sublime (using cmd+c) is 我从Sublime复制的代码(使用cmd + c)是

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
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 ] [这是我按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. 其他混合代码来自我昨天编写的某些python。 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. 从那时起,我已经使用了无数次复制/粘贴操作,所以我不明白为什么我最近的复制(cmd + v)会引导昨天的工作,同时出现明显的缩进错误,今天的代码等等。

Typing command+shift+v is no better: just different garbage. 键入command + shift + v并不更好:只是不同的垃圾。

What on earth is going on? 到底是怎么回事?

Mac OS X : 10.14.3 python installed by MacPorts Mac OS X:MacPorts安装的10.14.3 python
Tkinter not installed: I don't like IDLE. 未安装Tkinter:我不喜欢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. 我使用的配置类似于您的配置,命令行+ Xcode,我尝试将您的代码从IDE复制并粘贴到命令行中,并且一切正常。 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. 我将检查剪贴板(“查找器”->“编辑”->“显示剪贴板”)以查看代码是否被正确复制,它应该与在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. .py文件可以直接从命令行运行,而无需使用python3 path/to/file命令启动python解释器,为此,您可以将文件直接拖到终端中,它将自动输入路径。 This can be further simplified by using the interpreter shebang #! 通过使用解释器shebang #!可以进一步简化此操作#! in the .py file itself. 在.py文件本身中。 The path of the interpreter can be found by importing system import sys and print(sys.executable) . 可以通过导入系统import sysprint(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. 这给出了解释器的路径,然后将shebang添加到该路径,并将其放在.py文件的顶部,对我来说,它看起来像这样: #!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 ,但是由于是从MacPort安装的,因此可能有所不同。 The shebang allows the script to be run in the command line simply by entering the path to the file. 通过使用shebang,只需输入文件路径即可在命令行中运行脚本。 ie drag the file into terminal, hit enter, and it runs. 例如,将文件拖到终端中,按Enter键,它将运行。 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. 您还可以使用shift + up arrow的终端快捷方式,然后按Enter键再次运行最后一个命令,从而节省了将文件拖到终端中的时间。

OK, 好,

finally worked out that the pboard process was corrupted. 最终发现,pboard进程已损坏。
best way to fix is to kill the pboard process in activity monitor or terminal 修复的最佳方法是在活动监视器或终端中pboard进程

using 使用

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

All working well now. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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