简体   繁体   English

Python、Pandas 和 Pico:无法导入 Pandas,但 NumPy 没问题

[英]Python, Pandas, and Pico: Unable to import Pandas, but NumPy is no problem

I've managed to construct a simple app utilizing the Pico framework ( https://github.com/fergalwalsh/pico ).我已经设法利用 Pico 框架 ( https://github.com/fergalwalsh/pico ) 构建了一个简单的应用程序。 My frontend is connecting to my backend without any difficulties.我的前端连接到我的后端没有任何困难。 Below is my Python file, which at the moment simply returns/renders a string, using a client-side input value, "name".下面是我的 Python 文件,它目前只是使用客户端输入值“名称”返回/呈现一个字符串。

from __future__ import absolute_import
import sys
import pico
import numpy as np

# import sklearn
# import pandas as pd
from api2 import aloha

from pico import PicoApp

@pico.expose()
def hello(name):

   a = np.arange(15).reshape(3, 5)
   # a = np.arrange('data', 'field').reshape(3,5)

return "hello %s, %s" %(name, a)

app = PicoApp()
app.register_module(__name__)

(It also returns a NumPy array, simply because I'm testing what I can import into the file.) (它还返回一个 NumPy 数组,因为我正在测试可以导入到文件中的内容。)

All my packages are installed just fine, via Anaconda in /site-packages , which is in the python3.6 directory.我所有的包都安装得很好,通过/site-packages Anaconda,它位于python3.6目录中。

Oddly, the app runs fine;奇怪的是,该应用程序运行良好; it can import NumPy.它可以导入 NumPy。 It breaks, however, when I try to import Pandas or SKLearn.但是,当我尝试导入 Pandas 或 SKLearn 时,它会中断。 I've tried manually copying and pasting NumPy into /Library/Python/2.7/site-packages , which actually breaks the app.我试过手动将 NumPy 复制并粘贴到/Library/Python/2.7/site-packages ,这实际上破坏了应用程序。 But NumPy works in the app when it is only located in Anaconda's /site-packages .但是当 NumPy位于 Anaconda 的/site-packages时,它可以在应用/site-packages

I've tried altering app.register(__name__) to app.register('api') , which is the name of the Python file ( api.py ), based on another Question/Answer here.我已经尝试根据此处的另一个问题/答案将app.register(__name__)更改为app.register('api') ,这是 Python 文件 ( api.py ) 的名称。 I've also tried reinstalling Pandas with sudo -H pip install pandas , but all the requirements are already satisfied.我也尝试使用sudo -H pip install pandas重新安装 Pandas,但所有要求都已得到满足。

This is the error that is thrown when I try to include Pandas in api.py :这是我尝试在api.py包含 Pandas 时抛出的错误:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Library/Python/2.7/site-packages/pico/server.py", line 31, in <module>
    app = import_string(module_name)
  File "/Library/Python/2.7/site-packages/werkzeug/utils.py", line 443, in import_string
    sys.exc_info()[2])
  File "/Library/Python/2.7/site-packages/werkzeug/utils.py", line 431, in import_string
    module = import_string(module_name)
  File "/Library/Python/2.7/site-packages/werkzeug/utils.py", line 443, in import_string
    sys.exc_info()[2])
  File "/Library/Python/2.7/site-packages/werkzeug/utils.py", line 418, in import_string
    __import__(import_name)
  File "./api.py", line 6, in <module>
    import pandas as pd
  File "/Library/Python/2.7/site-packages/pandas/__init__.py", line 23, in <module>
    from pandas.compat.numpy import *
  File "/Library/Python/2.7/site-packages/pandas/compat/numpy/__init__.py", line 24, in <module>
    'this pandas version'.format(_np_version))
werkzeug.utils.ImportStringError: import_string() failed for 'api.app'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'api' not found.

Original exception:

ImportStringError: import_string() failed for 'api'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'api' not found.

Original exception:

ImportError: this version of pandas is incompatible with numpy < 1.9.0
your numpy version is 1.8.0rc1.
Please upgrade numpy to >= 1.9.0 to use this pandas version

When I run which python , it points to /Users/richardscheiwe/anaconda3/bin/python .当我运行which python ,它指向/Users/richardscheiwe/anaconda3/bin/python Also, I have NumPy v.1.15 installed, and I can't find any other NumPy folder(s).另外,我安装了 NumPy v.1.15,但找不到任何其他 NumPy 文件夹。 When I try moving a version of NumPy to Library/Python/2.7/site-packages , I get this error:当我尝试将 NumPy 的某个版本移动到Library/Python/2.7/site-packages ,出现此错误:

ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: cannot import name multiarray

I guess I need to somehow point the app's Python to Anaconda's Python 3.6 version, but I don't know how to do that.我想我需要以某种方式将应用程序的 Python 指向 Anaconda 的 Python 3.6 版本,但我不知道该怎么做。 Pico is also available in Anaconda's /site-packages directory, but it isn't pointing there. Pico 也在 Anaconda 的/site-packages目录中可用,但它没有指向那里。

Any help is greatly appreciated.任何帮助是极大的赞赏。 I've scoured StackOverflow and GitHub.我已经搜索过 StackOverflow 和 GitHub。

If I'm reading this correctly the error seems to come from trying to use a version of NumPy built for running on python 2.6 while your app is running using Python3.如果我没看错,错误似乎是由于在您的应用程序使用 Python3 运行时尝试使用为在 python 2.6 上运行而构建的 NumPy 版本。

Try removing NumPy using;尝试使用删除 NumPy; "sudo pip uninstall numpy" and then use "pip -H install Numpy" to try reinstalling it and seeing if it correctly finds the Python3 version of Numpy “sudo pip uninstall numpy”然后使用“pip -H install Numpy”尝试重新安装它并查看它是否正确找到了Python3版本的Numpy

You don't mention how you are starting the pico app but I assume you are doing like this:你没有提到你是如何启动 pico 应用程序的,但我假设你是这样做的:

python -m pico.server api

In this case it will simply use whatever python is in your path.在这种情况下,它将简单地使用您路径中的任何python If it is python3 in /Users/richardscheiwe/anaconda3/bin/python but you are getting errors referring to /Library/Python/2.7/ then there is some problem with your anaconda installation/paths in your environment.如果它是/Users/richardscheiwe/anaconda3/bin/python python3,但您收到引用/Library/Python/2.7/错误,那么您的环境中的 anaconda 安装/路径存在一些问题。

There is nothing different with pico to running a plain python script but I suggest you create a simplified script without pico (literally just import pandas ) to work out your environment issues with simpler error messages. pico 与运行普通的 python 脚本没有什么不同,但我建议您创建一个没有 pico 的简化脚本(实际上只是 import pandas ),以使用更简单的错误消息来解决您的环境问题。

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

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