I have a standard python proj package::
proj/
├── proj
│ ├── data
│ │ └── data.csv
│ ├── __init__.py
│ └── lib.py
├── MANIFEST.in
├── setup.py
└── tests
├── __init__.py
└── lib_test.py
data/data.csv
is installed with MANIFEST.in and include_package_data key in setup.py::
~/proj$ more setup.py
from setuptools import setup
setup(name='proj',
version=1.0,
packages=['proj'],
test_suite = 'tests',
include_package_data=True,
zip_safe=False)
~/proj$ more MANIFEST.in
include proj/data/*.csv
So after a python setup.py install
data.csv is in the place I want in site-packages::
$ tree ~/venv/lib/python2.7/site-packages/proj-1.0-py2.7.egg
~/venv/lib/python2.7/site-packages/proj-1.0-py2.7.egg
├── EGG-INFO
│ └── ...
└── proj
├── data
│ └── data.csv
├── __init__.py
└── ...
In setup.py, test_suite is declared, so when I call test from proj
first folder, this work well::
~/proj$ python setup.py test
running test
...
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
I would like to use in my unitest the data.csv
file from: ~/venv/lib/python2.7/site-packages/proj-1.0-py2.7.egg/proj/data/data.csv. In short a way to get ~/venv/lib/python2.7/site-packages/proj-1.0-py2.7.egg/proj/data/ folder.
With this code::
$ more tests/lib_test.py
import unittest, os, proj
from proj.lib import get_data
class TestUtils(unittest.TestCase):
def test_get_data(self):
datapath = os.path.dirname(os.path.abspath(proj.__file__)) + '/data'
data = '{}/data.csv'.format(datapath)
print '\n my data:', data
if __name__ == '__main__':
unittest.main()
Using __file__
I only succed to get ~/sandbox/proj/proj/data/data.csv ::
$ python setup.py test
running test
...
test_get_data (tests.lib_test.TestUtils) ...
my data: ~/sandbox/proj/proj/data/data.csv
ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
In fact
python setup.py test
does not load python package from the place python setup install
put it (~/venv/lib/python2.7/site-packages/proj-xxx), but from the place the python setup.py test
is run.
That's why with:
import proj, os
os.path.abspath(proj.__file__)
we can't catch the site-package
folder but only the working-copy files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.