Kab*_*esh 5 python csv packaging pypi
我在https://test.pypi.org上传了一个简单的 python 包。当我用 pip 下载它并尝试 yo run 我得到FileNotFoundError: [Errno 2] File b'data/spam_collection.csv' does not exist: b'data/spam_collection.csv'. 早些时候我在打包时上传 csv 文件时遇到问题。请参阅我在无法将 csv 文件上传到 test.pypi.org 中的问题。现在,在使用 pip 安装软件包后,我运行pip show -f bigramspamclassifier. 我得到了列出的 csv 文件。因此,我相信该文件已上传。我认为问题在于读取包中我的 python 文件中的文件。SpamClassifier.py 中 csv 文件的路径应该是什么?
pip show -f bigramspamclassifier
Version: 0.0.3
Summary: A bigram approach for classifying Spam and Ham messages
Home-page: ######
Author: #####
Author-email: #######
Location: /home/kabilesh/PycharmProjects/TestPypl3/venv/lib/python3.6/site-packages
Requires: nltk, pandas
Required-by:
Files:
bigramspamclassifier-0.0.3.dist-info/INSTALLER
bigramspamclassifier-0.0.3.dist-info/LICENSE
bigramspamclassifier-0.0.3.dist-info/METADATA
bigramspamclassifier-0.0.3.dist-info/RECORD
bigramspamclassifier-0.0.3.dist-info/WHEEL
bigramspamclassifier-0.0.3.dist-info/top_level.txt
bigramspamclassifier/SpamClassifier.py
bigramspamclassifier/__init__.py
bigramspamclassifier/__pycache__/SpamClassifier.cpython-36.pyc
bigramspamclassifier/__pycache__/__init__.cpython-36.pyc
bigramspamclassifier/data/spam_collection.csv
Run Code Online (Sandbox Code Playgroud)
我的项目文件结构
SpamClassifier.py 文件中 csv 的路径#这是我想知道的
def classify(self):
fullCorpus = pd.read_csv("data/spam_collection.csv", sep="\t", header=None)
fullCorpus.columns = ["lable", "body_text"]
Run Code Online (Sandbox Code Playgroud)
您的脚本正在尝试spam_collection.csv从相对路径加载文件。相对路径是相对于python被调用的位置加载的,而不是源文件所在的位置。
这意味着当您从bigramspamclassifier目录运行模块时,这将起作用。但是,一旦您的模块被pip安装,文件将不再与您运行代码的位置相关(它将被埋在您安装的库中的某处)。
您可以通过执行以下操作来加载相对于源文件:
import os
this_dir, this_filename = os.path.split(__file__)
DATA_PATH = os.path.join(this_dir, "data", "spam_collection.csv")
fullCorpus = pd.read_csv(DATA_PATH, sep="\t", header=None)
Run Code Online (Sandbox Code Playgroud)