0

i've created a pypi package that have some code that read a text file in it. However when i install my package from pip and import it to my code i get this error:

 FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt'

i know that why i have this error but i don't know how to solve it!

how can I read my text file in my package that won't cause a problem for using of the package from the outside?

i did what the answer of this link says: FileNotFoundError: [Errno 2] when packaging for PyPI

but it didn't work and gave me the same result! it gave me the same error. and actually there is indeed no file1.txt in my package folder and the error is correct. can anyone explain how to add it? or is there some other ways?

Here is my package's code and my outside of the package that installed the package's code:

import tensorflow as tf


def printRes():
    file1 = open("file1.txt", "r")
    sess = tf.compat.v1.Session()
    a = tf.constant(int(file1.readline(), 10))
    b = tf.constant(int(file1.readline(), 10))
    print(sess.run(a + b))

outside:

 from file1Text import demo
 demo.printRes()

thanks for your help.

Ali.shf
  • 1
  • 1
  • 3
    Possible duplicate of [FileNotFoundError: \[Errno 2\] when packaging for PyPI](https://stackoverflow.com/questions/56559186/filenotfounderror-errno-2-when-packaging-for-pypi) – Dustin Ingram Jul 09 '19 at 10:08
  • Use full path of file. I mean absolute path. – AHJeebon Jul 09 '19 at 11:36
  • Is your `setup.py` program using `include_package_data`? Is your file listed there or in `MANIFEST.in`? See https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files – deStrangis Jul 09 '19 at 11:54

0 Answers0