FileNotFoundError:打包PyPI时出现[Errno 2] [英] FileNotFoundError: [Errno 2] when packaging for PyPI

查看:134
本文介绍了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文件的路径应该是什么?

I have uploaded a simple python package in 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. Now after installing the package with pip I run pip show -f bigramspamclassifier. I get the csv file listed. Therefore, I believe the file has been uploaded. I think the issue is with reading the file in my python file in the package. What should be the path to the csv file in SpamClassifier.py?

pip show -f bigramspamclassifier

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

SpamClassifier.py文件中csv的路径#这是我想知道的

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的位置加载的,相对于不是的源文件所在的位置.

Your script is attempting to load the spam_collection.csv file from a relative path. Relative paths are loaded relative to where python is being invoked, not where the source file is.

这意味着当您从bigramspamclassifier目录运行模块时,它将起作用.但是,一旦模块被pip安装,文件将不再与您在其上运行代码的位置有关(它将被埋在已安装的库中的某个位置).

This means that when you're running your module from the bigramspamclassifier directory, this will work. However, once your module is pip-installed, file will no longer be relative to where you're running your code from (it will be buried somewhere in your installed libraries).

您可以执行以下操作来相对于源文件进行加载:

You can instead load relative to the source file by doing something like:

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)

这篇关于FileNotFoundError:打包PyPI时出现[Errno 2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆