FileNotFoundError: [Errno 2] when packaging for PyPI

FileNotFoundError: [Errno 2] when packaging for PyPI

我在 https://test.pypi.org. When I download this with pip and try yo run I get FileNotFoundError: [Errno 2] File b'data/spam_collection.csv' does not exist: b'data/spam_collection.csv'. Earlier I had issues with uploading the csv file when packaging. See my question in Could not upload csv file to test.pypi.org 中上传了一个简单的 python 包。现在用 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

My project file structure

Path to csv in SpamClassifier.py file #This what I want to know

    def classify(self):
    fullCorpus = pd.read_csv("data/spam_collection.csv", sep="\t", header=None)
    fullCorpus.columns = ["lable", "body_text"]

您的脚本正试图从相对路径加载 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)