1

Anytime when i just try to call pandas module, i have the error:

    import pandas as pd
ValueError: source code string cannot contain null bytes.

I am using VS Code to edit the code. I am sure the file does not contain null bytes, because I rewrote the code and even this import by itself causes the problem.

I have already tried reinstalling Python and VS Code, and I made sure to choose 'add to path' when reinstalling Python.

How can I fix the problem?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Trepetaky
  • 45
  • 3
  • 1
    I'm willing to bet you're accidentally saving the file as UTF-16, which is covered in [this answer](/a/56573475/4518341) on the question I linked. BTW, welcome to Stack Overflow! Check out the [tour] and [ask]. In the future, please start with your own research. I found that question by googling "python" plus the error message. – wjandrea May 28 '22 at 22:58
  • Some text encodings are not permissible for Python source code. It is generally best to use `utf-8` text encoding, but most importantly, do not use `utf-16` or `utf-32` since it will use a lot of null bytes. Check the editor settings to make sure the encoding is suitable. – Karl Knechtel May 28 '22 at 22:58
  • @wjandrea Nice find - the duplicates I turned up were not as good quality. After the edits, I think this question is a good alias for the canonical. – Karl Knechtel May 28 '22 at 22:59
  • What file do you mean? Where I write my code? I think that a main problem isnt how i create file. Because i have some old files with complted code and when i just add comman 'import pandas', i get the error 'source code string cannot contain null bytes'. Without command 'import pandas',code works – Trepetaky May 28 '22 at 23:01
  • @wjandrea Hey man, how can i save or resave file in UTF-8 format? – Trepetaky May 28 '22 at 23:09
  • @Trepetaky Yes, the script you're writing. Resaving as UTF-8 is explained in the answer I linked. Did you try it already? – wjandrea May 28 '22 at 23:12
  • "What file do you mean? Where I write my code?" Yes. You should check your editor settings, so that it saves the file in a proper encoding. How to do this with your editor is a tech support question. [Please try](https://meta.stackoverflow.com/questions/261592) to answer it [with research](https://duckduckgo.com/?q=visual+studio+code+encoding). – Karl Knechtel May 28 '22 at 23:44
  • @wjandrea Guys, it didn't help. I saw you answer and i found out how my files are saving in utf 8, so, there is another problem which i dont know – Trepetaky May 29 '22 at 08:22
  • @Karl Knechtel Hey))) watch what i wrote – Trepetaky May 29 '22 at 08:23
  • @Trepetaky OK, then try out the other answers on that question. There are a bunch of different suggestions. Although some might not be applicable, like the one about virtualenv; use your best judgement. If none of them work, [edit] your question to include what you tried and what happened, and we'll reopen it. Problems like this unfortunately can be difficult to pin down, so you gotta try whatever you can. – wjandrea May 29 '22 at 13:23
  • 1
    @ wjandrea 1 days later i solve this problem. TO be honest i didnt understand how i solved it to the end. But I deleted python again, istalled earlier version and opted for all extra options which installer window sugested to me. Then new varian of configurator(global python) appear in the VS and i chose it. Finally VS is working – Trepetaky May 29 '22 at 15:33

0 Answers0