FileNotFoundError:打包PyPI时出现[Errno 2]
我在https://test.pypi.org中上传了一个简单的python包。当我用pip下载此文件并尝试运行时,我得到了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文件的路径应该是什么?
点显示-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
我的项目文件结构
SpamClassifier.py文件中csv的路径#这是我想知道的
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)