UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character upon running a PyInstaller-compiled script

20,011

Solution 1

I had the same error message. I fixed it by replacing \ by \\ in the string containing the path of the file to open.

Solution 2

I think another elegent way is to combine the pd.read_csv with the open function. For instance, if I have a csv file called Ürümqi Diwopu International Airport.csv, I could use the following code:

import pandas as pd

dataframe = pd.read_csv(open(os.path.join(datapath, "Ürümqi Diwopu International Airport.csv"), 'r', encoding='utf-8'), index_col=0)

Hope this could help~

Share:
20,011
Luke25361
Author by

Luke25361

I'm currently trying to create simple programs with GUIs using Tkinter in Python.

Updated on January 15, 2021

Comments

  • Luke25361
    Luke25361 over 3 years

    I've just finished a program I've been working on and have been wanting to compile it to a single .exe file for distribution. I decided to use pyinstaller 3 as it has worked for me before however upon successfully compiling my file I get the following message upon running the program.

    C:\Users\Luke\Documents\program\dist>viewbot.exe
    _ctypes
    C:\Users\Luke\AppData\Local\Temp\_MEI59042\_ctypes.pyd
    _tkinter
    C:\Users\Luke\AppData\Local\Temp\_MEI59042\_tkinter.pyd
    Traceback (most recent call last):
      File "<string>", line 7, in <module>
      File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
      File "C:\Users\Luke\Documents\program\PyInstaller\loader\pyi_importers.py", line 302, in load_module
      File "C:\Python33\lib\tkinter\__init__.py", line 40, in <module>
        import _tkinter # If this fails your Python may not be configured for Tk
      File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
      File "C:\Users\Luke\Documents\program\PyInstaller\loader\pyi_importers.py", line 474, in load_module
    UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
    

    If it helps, this is a minimal code example of my program:

    import os
    import subprocess
    from subprocess import call
    import time
    import tkinter
    from tkinter import filedialog
    call("color a", shell=True)
    root = tkinter.Tk()
    root.withdraw()
    print ("Please locate your firefox browser)")
    path = filedialog.askopenfilename(parent=root,title="Please locate your firefox browser")
    path = path + " {0}"
    FNULL = open(os.devnull, 'w')
    viewed = 0
    url = "http://google.com"
    refresh = 15
    views = 5
    call("cls", shell=True)
    for i in range(views):
        proc = subprocess.Popen(path.format(url))
        time.sleep(refresh)
        viewed = viewed + 1
        print ("Viewed", viewed, "time")
        proc.terminate()
        call("cls", shell=True)
    print ("Viewing finished in", refresh * views, "seconds")
    time.sleep(5)
    

    Not quite sure what to do here so any help would be much appreciated :)

  • Luke25361
    Luke25361 over 8 years
    Hi, thanks for the reply! What do you mean by the string containing the path to the file to import?
  • Pierre Carbonnelle
    Pierre Carbonnelle over 8 years
    I had the same error as yours, but in a different use case without pyinstaller. I was trying to import a csv file into pandas. Initially, I thought that the problem was in the csv file, but eventually I found out that it was in the path of the file. Using \\ fixed it. Not sure how it applies in your case.
  • Gorkem
    Gorkem about 3 years
    Thanks, this solved my issue too for Turkish.. Setting encoding in pandas.read_csv has no effect in filename it does effect file contents only. Example filename : "SomeTurkish_ParçaŞĞİL\dataset_csv.csv" .