[英]script works in Jupyter notebook, fails from command line or Atom IDE
I have python installed via Anaconda, and want to serialize a rather large dataframe to a disk file.我通过 Anaconda 安装了 python,并且想将一个相当大的数据帧序列化到一个磁盘文件。 This runs well on a Jupyter notebook, but fails when run from the Atom IDE or the command prompt (ie python script_name.py)这在 Jupyter 笔记本上运行良好,但从 Atom IDE 或命令提示符(即 python script_name.py)运行时失败
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['a', 'b', 'c'])
df.to_hdf('data.h5', key='df', mode='w')
pd.read_hdf('data.h5')
Here is the error:这是错误:
line 3, in <module>
df.to_hdf('data.h5', key='df', mode='w')
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2530, in to_hdf
pytables.to_hdf(path_or_buf, key, self, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 276, in to_hdf
path_or_buf, mode=mode, complevel=complevel, complib=complib
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 484, in __init__
tables = import_optional_dependency("tables")
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\compat\_optional.py", line 93, in import_optional_dependency
raise ImportError(message.format(name=name, extra=extra)) from None
ImportError: Missing optional dependency 'tables'. Use pip or conda to install tables.
I added the hdf5.dll path to the list of environment variables but no luck.我将 hdf5.dll 路径添加到环境变量列表中,但没有运气。 Both pip and conda tell me that I have a version of hdf5 and tables so I do not understand how this code can run in Jupyter notebook but not outside the notebook. pip 和 conda 都告诉我我有一个 hdf5 和表的版本,所以我不明白这段代码如何在 Jupyter notebook 中运行,但在 notebook 之外运行。 BTW, I have a similar issue with matplotlib and a few other packages which run without errors from a Jupyter notebook but throw up package not found, dll missing errors when run from the command line.顺便说一句,我在 matplotlib 和其他一些包中遇到了类似的问题,这些包在 Jupyter 笔记本上运行时没有错误,但是找不到包,从命令行运行时 dll 丢失错误。 Can anyone please help?有人可以帮忙吗? Thank you!谢谢!
My path looks like:我的路径看起来像:
c:\users\UserID\appdata\roaming\python\python37\site-packages;
C:\ProgramData\Anaconda3\pkgs\m2w64-openssl-1.0.2.g-2\Library\mingw-w64\bin;
C:\ProgramData\Anaconda3\pkgs\pytables-3.6.1-py37h1da0976_0\Lib\site-packages\tables\;
C:\ProgramData\Anaconda3\Lib\site-packages\tables;
C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;
C:\ProgramData\Anaconda3\Library\usr\bin;
C:\Users\UserID\AppData\Local\atom;
C:\ProgramData\Anaconda3\Library\mingw-w64\bin;
C:\Program Files (x86)\ActiveState Komodo Edit 11\;
C:\ProgramData\Anaconda3\envs\pudl;
C:\Users\UserID\AppData\Roaming\Python\Python37\Scripts;
C:\WINDOWS\system32;
C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
......
C:\Users\UserID\AppData\Local\atom\bin
OS: Win 10 Python version: 3.7操作系统:Win 10 Python 版本:3.7
You probably need to activate the environment where your Jupyter kernel resides, as that will be pulling in the necessary dependencies on your behalf.您可能需要激活 Jupyter 内核所在的环境,因为这将代表您引入必要的依赖项。
Assuming you've already created an environment using conda create -n my_conda_env
you'll also have to activate it using conda activate -n my_conda_env
then your cmd prompt will be prefixed with the name of the environment.假设您已经使用conda create -n my_conda_env
创建了一个环境,您还必须使用conda activate -n my_conda_env
来激活它,那么您的 cmd 提示符将以环境名称为前缀。
If you're not running in an environment - I'd really recommend it - as it can avoid some dependency issues.如果您不在环境中运行 - 我真的推荐它 - 因为它可以避免一些依赖性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.