Python:源代码字符串不能包含空字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31233777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:40:16  来源:igfitidea点击:

Python: source code string cannot contain null bytes

pythonmacoszelle-graphics

提问by Peter G. Williams

I'm using Max OS X 10.10.3, and I finally got the graphics.pyto show in Python 3, before it was saying no module existed.

我使用的是 Max OS X 10.10.3,我终于graphics.py在 Python 3 中显示了它,然后它说不存在模块。

However, now when I try import graphics, or from graphics import *, I get the message:

但是,现在当我尝试import graphics, 或 时from graphics import *,我收到消息:

"source code string cannot contain null bytes"

Does any Mac user (using Python 3) perhaps know what is wrong? Has anyone used the Zelle book and his graphics.pymodule? Thanks.

是否有任何 Mac 用户(使用 Python 3)可能知道出了什么问题?有没有人使用过 Zelle 的书和他的graphics.py模块?谢谢。

采纳答案by DilithiumMatrix

For posterity: I had the same problem and fixed it using,

对于后代:我遇到了同样的问题并使用修复了它,

sed -i 's/\x0//g' FILENAME

The file seemed to be messed up in numerous ways (wrong endings, etc); no idea how...

该文件似乎在很多方面都搞砸了(错误的结尾等);不知道怎么...

See https://stackoverflow.com/a/2399817/230468

https://stackoverflow.com/a/2399817/230468

回答by Peter G. Williams

This kind of error is not from your project source code. This kind of error emerges from your python interpreter. So the best solution is to set your python interpreter in your project envdirectory. or set the interpreters virtual env properly using your IDE's interpreter configuration.

这种错误不是来自您的项目源代码。这种错误出现在您的 Python 解释器中。所以最好的解决方案是在你的项目env目录中设置你的 python 解释器。或使用 IDE 的解释器配置正确设置解释器虚拟环境。

回答by user2215619

I've got the same error and the solution is to uninstall the Library and if you can't simply delete it and reinstall the Library. should work it has work for me...

我遇到了同样的错误,解决方案是卸载库,如果您不能简单地删除它并重新安装库。应该工作它对我有用......

回答by Chaplin Hwang

I just encountered this problem, which is usually caused by the encoding format. You can use Notepad++ to change the encoding format of python files to UTF-8.

我刚遇到这个问题,一般是编码格式问题。您可以使用 Notepad++ 将 python 文件的编码格式更改为 UTF-8。

回答by Fabio Caccamo

Open your file with an editor that can show you all invisible character. You will see where is the invalid char, just delete and re-type it.

使用可以显示所有隐形字符的编辑器打开您的文件。您将看到无效字符在哪里,只需删除并重新键入即可。

If you are on mac you can do it with Coda > Open your file > Show invisible characters.

如果您使用的是 mac,您可以使用 Coda > 打开您的文件 > 显示不可见字符来完成。

回答by Paco Meraz

I am using Visual Studio Code, the encoding was set to UTF-16 LE. You can check the encoding on the right bottom side of VSCode. Just click on the encoding and select "save with encoding" and select UTF-8. It worked perfectly.

我使用的是 Visual Studio Code,编码设置为 UTF-16 LE。您可以在 VSCode 的右下角检查编码。只需单击编码并选择“使用编码保存”并选择 UTF-8。它工作得很好。

回答by Atharva Panage

  • Go to python folder python36/Lib/site-packages/scipy/
  • Open __init__.py
  • 转到python文件夹 python36/Lib/site-packages/scipy/
  • 打开 __init__.py

Change:

改变:

from scipy._lib._testutils import PytestTester

to:

到:

from scipy._lib.test__testutils import PytestTester

It worked on my windows 7

它适用于我的 Windows 7

回答by Habib Karbasian

I got this message when I wanted to use evalfor my input for my function that sometimes it takes string or int/float but when it takes numpynumbers, it throws this exception, eval(number).

当我想eval用于我的函数的输入时,我收到了这条消息,有时它需要字符串或整数/浮点数,但是当它需要numpy数字时,它会抛出这个异常,eval(number).

My solution was eval(str(number)).

我的解决方案是eval(str(number)).