NameError:在自定义包中调用函数时未定义名称“pd”

dac*_*aco 3 python pandas

语境

我正在学习用于数据科学的 python,并且正在使用 Foursquare API 来探索坐标附近的场所。它返回一个 JSON 文件,因此我创建了一个函数,使用“foursquare”包 (github.com/mLewisLogic/foursquare) 从 Foursquare 结果返回 Pandas DataFrame,然后将数据提取附加到 DataFrame。

该函数在我的 Jupyter Notebook 中运行(您可以在此处查看该函数https://github.com/dacog/foursquare_api_tools/blob/master/foursquare_api_tools/foursquare_api_tools.py),我想让其他人更容易并尝试创建可以直接从 github 使用 pip 安装的包。我成功创建了一个包并将其发布到 github 来测试它,但是当我尝试使用该函数时它返回

NameError: name 'pd' is not defined
Run Code Online (Sandbox Code Playgroud)

试用该包的步骤

!pip install git+https://github.com/dacog/foursquare_api_tools.git#egg=foursquare_api_tools

# @hidden_cell
CLIENT_ID = 'Secret' # your Foursquare ID
CLIENT_SECRET = 'Secret' # your Foursquare Secret
VERSION = '20180605' # Foursquare API version

from foursquare_api_tools import foursquare_api_tools as ft

ft.venues_explore(client,lat='40.7233',lng='-74.0030',limit=100)
Run Code Online (Sandbox Code Playgroud)

我得到

NameError                                 Traceback (most recent call last)
<ipython-input-47-0a062ed9d667> in <module>()
      3 import pandas as pd
      4 
----> 5 ft.venues_explore(client,lat='40.7233',lng='-74.0030',limit=100)

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/foursquare_api_tools/foursquare_api_tools.py in venues_explore(client, lat, lng, limit)
      3     This returns a pandas dataframe with name, city ,country, lat, long, postal code, address and main category as columns'''
      4         # creata a dataframe
----> 5         df_a = pd.DataFrame(columns=['Name', 'City', 'Latitude','Longitude','Category','Postal Code', 'Address'])
      6         ll=lat+','+lng
      7         #get venues using client https://github.com/mLewisLogic/foursquare

NameError: name 'pd' is not defined
Run Code Online (Sandbox Code Playgroud)

import pandas as pd在主笔记本中的函数内部尝试了 __init__.py ,总是得到相同的结果。

您可以在https://github.com/dacog/foursquare_api_tools检查代码

这是我第一次创建一个包,而且对 python 还很陌生,所以任何帮助将不胜感激。

更新 当我进行测试时,Pandas 在环境中运行良好。 在此输入图像描述

安装的Python版本是:

!which python --> /home/jupyterlab/conda/bin/python

!whereis python
/usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /etc/python /etc/python2.7
/usr/local/lib/python2.7 /usr/share/python
/home/jupyterlab/conda/bin/python /home/jupyterlab/conda/bin/python3.6
/home/jupyterlab/conda/bin/python3.6-config /home/jupyterlab/conda/bin/python3.6m /home/jupyterlab/conda/bin/python3.6m-config /usr/share/man/man1/python.1.gz
Run Code Online (Sandbox Code Playgroud)

suv*_*ayu 5

您缺少import pandas as pd中的声明foursquare_api_tools.py。只需将该行添加到该文件的顶部,您就可以开始了。

线索就在 error:NameError中,在第 5 行调用 的地方pd.DataFrame,因为没有 import 语句,Python 不知道“名称”的pd含义。