简体   繁体   English

Python:导入 cx_Oracle 导入错误:没有名为 cx_Oracle 的模块错误被抛出

[英]Python: import cx_Oracle ImportError: No module named cx_Oracle error is thown

I try to write a script in .py for oracle connectivity:我尝试在 .py 中编写一个用于 oracle 连接的脚本:

#!/usr/bin/python

import cx_Oracle

connstr='username/pwd@database'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from table1;')
print curs.description
for row in curs:
   print row
conn.close()

I get the following error:我收到以下错误:

Traceback (most recent call last):
  File "test_SQLPython.py", line 3, in ?
    import cx_Oracle
ImportError: No module named cx_Oracle

Any help would be appreciated?任何帮助,将不胜感激? Thanks.谢谢。

Tried installing it via rpm posted in above answers, but it didn't worked.尝试通过上述答案中发布的 rpm 安装它,但没有奏效。 What worked instead is plain pip install .代替的是普通的pip install

pip install cx_oracle

The above command installed cx_oracle=6.1 Please note that I'm using python 2.7.14 Anaconda release and oracle 12c .上面的命令安装了cx_oracle=6.1请注意,我使用的是python 2.7.14 Anaconda releaseoracle 12c

Windows help: Windows 帮助:

  1. Get the instant client from here .这里获取即时客户端。
  2. Put the directory into your PATH variable.将目录放入 PATH 变量中。
  3. Go to the command prompt ( Win + R and type cmd ) and set 2 variables matching your location- for example:转到命令提示符( Win + R并键入cmd )并设置与您的位置匹配的 2 个变量 - 例如:

    set TNS_ADMIN=C:\\instant_client\\instantclient_11_2 set ORACLE_HOME=C:\\instant_client\\instantclient_11_2 set TNS_ADMIN=C:\\instant_client\\instantclient_11_2 set ORACLE_HOME=C:\\instant_client\\instantclient_11_2

Then install the cx_Oracle module from an exe.然后从 exe 安装 cx_Oracle 模块。 If you use pip or easy_install, ...good luck.如果您使用 pip 或 easy_install,...祝您好运。

You can get the installer here: https://pypi.python.org/pypi/cx_Oracle/5.1.3您可以在此处获取安装程序: https : //pypi.python.org/pypi/cx_Oracle/5.1.3

For me the problem was that I had installed cx_Oracle via DOS pip which changed it to lower case.对我来说,问题是我通过 DOS pip 安装了 cx_Oracle,将其更改为小写。 Installing it through Git Bash instead kept the mixed case.通过 Git Bash 安装它而不是保留混合大小写。

I have just faced the same problem.我刚刚遇到了同样的问题。 First, you need to install the appropriate Oracle client for your OS.首先,您需要为您的操作系统安装合适的 Oracle 客户端。 In my case, to install it on Ubuntu x64 I have followed this instructions https://help.ubuntu.com/community/Oracle%20Instant%20Client#Install_RPMs就我而言,要在 Ubuntu x64 上安装它,我已按照此说明进行操作https://help.ubuntu.com/community/Oracle%20Instant%20Client#Install_RPMs

Then, you need to install cx_Oracle, a Python module to connect to the Oracle client.然后,您需要安装 cx_Oracle,这是一个用于连接到 Oracle 客户端的 Python 模块。 Again, assuming you are running Ubuntu in a 64bit machine, you should type in a shell:同样,假设您在 64 位机器上运行 Ubuntu,您应该输入一个 shell:

wget -c http://prdownloads.sourceforge.net/cx-oracle/cx_Oracle-5.0.4-11g-unicode-py27-1.x86_64.rpm
sudo alien -i cx_Oracle-5.0.4-11g-unicode-py27-1.x86_64.rpm

This will work for Oracle 11g if you have installed Python 2.7.x, but you can download a different cx_Oracle version in http://cx-oracle.sourceforge.net/ To check which Python version do you have, type in a terminal:如果您安装了 Python 2.7.x,这将适用于 Oracle 11g,但您可以在http://cx-oracle.sourceforge.net/下载不同的 cx_Oracle 版本要检查您拥有哪个 Python 版本,请在终端中键入:

python -V

I hope it helps我希望它有帮助

I had a similar problem, you gotta make sure you have:我有一个类似的问题,你必须确保你有:

  1. oracle instant client甲骨文即时客户端
  2. cx_Oracle binary( from SourceForge ) cx_Oracle 二进制文件(来自 SourceForge)
  3. Python IMPORTANT: Make sure they are ALL either 64-bit or 32-bit, mixing is gonna cause problems Python 重要提示:确保它们都是 64 位或 32 位,混合会导致问题

Windows and Anaconda help Windows 和 Anaconda 帮助

Anaconda 4.3.0 comes with Python 3.6 as the root. Anaconda 4.3.0 以 Python 3.6 为根。 Currently cx_Oracle only supports up to 3.5.目前 cx_Oracle 最多只支持 3.5。 I tried creating 3.5 environment in envs, but when running cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe it installs in root only against 3.6我尝试在 envs 中创建 3.5 环境,但是在运行 cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe 时,它​​仅针对 3.6 安装在 root 中

Only workaround I could find was to change the root environment from 3.6 to 3.5:我能找到的唯一解决方法是将根环境从 3.6 更改为 3.5:

activate root
conda update --all python=3.5

When that completes run cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe.完成后运行 cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe。

Tested it with import and worked fine.使用导入对其进行了测试并且运行良好。

import CX_Oracle

Unknown92 answer helped me ( on windows ). Unknown92 答案帮助了我(在 Windows 上)。 Note that all versions must fit.请注意,所有版本都必须适合。

I've downloaded cx_Oracle here , for me the file cx_Oracle-5.2.1-12c.win-amd64-py3.5.exe worked with:我在这里下载了cx_Oracle ,对我来说,文件 cx_Oracle-5.2.1-12c.win-amd64-py3.5.exe 使用过:

  • Python 3.5.1 64bit. Python 3.5.1 64 位。 for some reason the default download link from the main page is for the 32bit version.出于某种原因,主页上的默认下载链接是针对 32 位版本的。
  • And oracle instance client 12.1.0.2.0 (the file named instantclient-basic-windows.x64-12.1.0.2.0.zip)here .和 oracle 实例客户端 12.1.0.2.0(名为 Instantclient-basic-windows.x64-12.1.0.2.0.zip 的文件)在这里

In my case the solution was to use:在我的情况下,解决方案是使用:

python3 -m pip install cx_Oracle --upgrade --user

instead of代替

pip3 install cx_Oracle

It was caused by some mess in pip , so I haven't got cx_Oracle installed properly:这是由pip的一些混乱引起的,所以我没有正确安装cx_Oracle

# python3 -m pip -V 
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

# pip3 -V
pip 21.2.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

I hope it'll save someone a few hours of life...我希望它可以挽救某人几个小时的生命......

To access Oracle from python you need (additionally) the cx_Oracle module .要从 python 访问 Oracle,您需要(另外) cx_Oracle 模块 The module must be located either in the system python path or you have to set the PYTHONPATH appropriate.该模块必须位于系统 python 路径中,或者您必须适当地设置 PYTHONPATH。

Although silly mistake but make sure to use correct module name and respect capitalization虽然犯了愚蠢的错误,但请确保使用正确的模块名称并尊重大小写

I installed this package via command line as pip install cx_oracle in my windows machine.我通过命令行安装了这个包,作为pip install cx_oracle在我的 Windows 机器上。 While importing it in spyder as cx_oracle , it kept on giving following error:在 spyder cx_oracle其作为cx_oracle导入时,它不断给出以下错误:

ModuleNotFoundError: No module named 'cx_oracle' . ModuleNotFoundError: No module named 'cx_oracle'

Upon correcting the module name in import command to cx_Oracle (ie capital letter 'O' in oracle), it was a successful import.将导入命令中的模块名称更正为cx_Oracle (即oracle中的大写字母'O')后,导入成功。

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

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