Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

python - FileNotFoundError: [Errno 2] when packaging for PyPI

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

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="", header=None)
    fullCorpus.columns = ["lable", "body_text"]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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="", header=None)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...