Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="\t", header=None)
    fullCorpus.columns = ["lable", "body_text"]
like image 250
Kabilesh Avatar asked Jun 12 '19 09:06

Kabilesh


1 Answers

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="\t", header=None)
like image 139
Dustin Ingram Avatar answered Oct 21 '22 03:10

Dustin Ingram