Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeDecodeError when using cx_Oracle.connect #71

Closed
Heloisej opened this issue Aug 23, 2017 · 23 comments
Closed

UnicodeDecodeError when using cx_Oracle.connect #71

Heloisej opened this issue Aug 23, 2017 · 23 comments

Comments

@Heloisej
Copy link

Heloisej commented Aug 23, 2017

I installed cx_Oracle via pip and successfully import it. However when I use the function cx_Oracle.connect, the error occured. The following is my code:

import os
os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.ZHS16GBK'
import cx_Oracle
connection = cx_Oracle.connect("uname/pwd@localhost/orcl")

The following is the error message:

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 82-83: invalid continuation byte

I almost have tried all the methods that can be found on Google, but the question remains unsolved.

Environment:

  • Python: Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
  • cx_Oracle: 6.0.1(32 bit)
  • Oracle client version: Instant Client 11.2(32 bit)
  • Oracle database version: 11.2
  • Windows 7 Service Pack 1 (64 bit)

oracle defualt encode
1

my python default encode
2

@cjbj
Copy link
Member

cjbj commented Aug 23, 2017

Thanks for the details:

Just checking: how exactly did you install it? Did you use the --upgrade option?

How do you know it's 6.0.1? What does cx_Oracle.version give?

[edited]

@anthony-tuininga
Copy link
Member

In the command prompt where you are running Python, set the environment variable DPI_DEBUG_LEVEL to the value 31, as in: set DPI_DEBUG_LEVEL=31. Then let us know the results. Thanks!

@Heloisej Heloisej changed the title UnicodeDecodeError when using cx_Oracla.connect UnicodeDecodeError when using cx_Oracle.connect Aug 24, 2017
@Heloisej
Copy link
Author

When I first installed cx_Oracle, I just used

pip install cx_Oracle

I tried to add --upgrade then, it showed that

Requirement already up-to-date

Then I checked my cx_Oracle version by using cx_Oracle.version, and it is 6.0.1


I also tried to set the environment variable DPI_DEBUG_LEVEL to the value 31 in the command prompt. Unfortunately, it still didn't work. The same error message appeared when using the function cx_Oracle.connect.

@cjbj
Copy link
Member

cjbj commented Aug 24, 2017

One more thing to check is that you have the correct VS Redistributable: you need Microsoft Visual Studio 2005 Redistributable for the 11.2 client. And make sure that the expected Oracle Client is first in your PATH.

It would be nice to know why a clearer error message isn't displayed.

@jmcp
Copy link

jmcp commented Aug 24, 2017 via email

@anthony-tuininga
Copy link
Member

I also tried to set the environment variable DPI_DEBUG_LEVEL to the value 31 in the command prompt. Unfortunately, it still didn't work. The same error message appeared when using the function cx_Oracle.connect.

Did you not get any additional output?

Can you also try the following?

cx_Oracle.connect("user/password@server/service", encoding = "UTF-8", nencoding = "UTF-8")

This will bypass the NLS_LANG environment variable completely.

@jmcp
Copy link

jmcp commented Aug 24, 2017 via email

@wanrenjerry
Copy link

have you solved the above questions? I encountered the same problem

@anthony-tuininga
Copy link
Member

Did you try the suggestions made above? If so, please report on your results. Thanks!

@Heloisej
Copy link
Author

Heloisej commented Aug 25, 2017

Sorry for the late response.

I successfully solved the problem!! What I did is as below:

  1. Download and install Microsoft Visual C++ 2005 Redistributable Package (x86)
  2. Set the expected Oracle Client first in my PATH.
  3. Using cx_Oracle.connect("user/password@server/service", encoding = "UTF-8", nencoding = "UTF-8")

Then it works!!
image

Thank you for all the suggestions and solutions provided!

@cjbj
Copy link
Member

cjbj commented Aug 25, 2017

@Heloisej do you still get the error if you don't set the encoding & nencoding parameters?
We'd like to be able to fix what is causing the UnicodeDecodeError and display the proper underlying OS error message.

@Heloisej
Copy link
Author

I deleted the encoding & nencoding parameters. It still works. So I think the key issue is to install the right Microsoft Visual C++ Redistributable Package

@anthony-tuininga
Copy link
Member

I'm glad you got it resolved. When you set the environment variable DPI_DEBUG_LEVEL to the value 31, did you not get any output at all? We would definitely like to resolve this for everyone!

Perhaps you can also let us know what language/regional parameters you have set in your OS?

@Heloisej
Copy link
Author

  1. When I set DPI_DEBUG_LEVEL=31, there is no output.
  2. The language/regional parameters I set is Simplified Chinese, China.

@anthony-tuininga
Copy link
Member

anthony-tuininga commented Aug 25, 2017

Ok. Thanks. You should see some output. Are you running Python from the command line? Not something like IPython, Anaconda, etc.? If you run this in Python do you see the expected output of '31'?

os.environ["DPI_DEBUG_LEVEL"]

Of course you'd have to introduce the problem again. So if you don't want to do that, I understand. Hopefully someone else who has this problem as well will follow these instructions and provide the information we are looking for in that case!

@shaojianwei
Copy link

I follow the suggestions above, but the problem remains.

@anthony-tuininga
Copy link
Member

anthony-tuininga commented Sep 11, 2017

Can you show the output where you set the environment variable DPI_DEBUG_LEVEL to the value 31, the output you get when you run the Python statement os.environ["DPI_DEBUG_LEVEL"] and the output when you run the Python statements to import cx_Oracle and attempt to make the connection? Please also show the Python version and cx_Oracle version that you are using.

@fengxing11111
Copy link

fengxing11111 commented Sep 15, 2017

i also don't know how to deal with the problem ,still UnicodeDecodeError.
and i meet the problem when use pandas data.to_sql('data',engine)

@anthony-tuininga
Copy link
Member

Can you show the output when you set the environment variable DPI_DEBUG_LEVEL to the value 31?

@cjbj
Copy link
Member

cjbj commented Sep 15, 2017

@fengxing11111 Are you asking about an error when you execute a particular statement with specific data? This current issue is about the error at initial connection. Can you confirm? If you are asking about execution, then please create a new issue.

@fengxing11111
Copy link

qq 20170918193919
qq 20170918194227
qq 201709181943152
Environment:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
cx_Oracle: 6.0.2
Oracle client version: Instant Client 11.2(32 bit)
Oracle database version: 11.2

@anthony-tuininga
Copy link
Member

If you are running a 64-bit Python and cx_Oracle, you also need to have 64-bit Instant Client and from what you have stated above you have 32-bit Instant Client!

Also, can you set DPI_DEBUG_LEVEL=31 before you import cx_Oracle. Preferably before you even start Python? Thanks.

@fengxing11111
Copy link

Thanks for your reply!Maybe the wrong environment construction.I change the python version to 32-bit and i can run the codes.I will change the environments to 64-bit to test whether the codes can run in the next few days.Thanks a lot.This is my first time asking question online by English,hope you can understand it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants