FileNotFoundError:[Errno 2]打包PyPI时

2024-09-22 14:33:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在https://test.pypi.org中上传了一个简单的python包。当我用pip下载这个并试着运行时,我得到了FileNotFoundError: [Errno 2] File b'data/spam_collection.csv' does not exist: b'data/spam_collection.csv'。之前我在打包时上传csv文件时遇到了问题。请看Could not upload csv file to test.pypi.org中我的问题。现在,在用pip安装包之后,我运行pip show -f bigramspamclassifier。我得到csv文件列表。因此,我相信文件已经上传了。我认为问题在于如何读取包中python文件中的文件。csv文件的路径应该是什么SpamClassifier.py?你知道吗

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

enter image description here

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"]

Tags: pip文件csvtopytestinfopypi
1条回答
网友
1楼 · 发布于 2024-09-22 14:33:15

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

相关问题 更多 >