简体   繁体   中英

Python unittest extension module relative import 'proper' handling

Given a module setup as follows:

myproject
├── MANIFEST.in
├── README.md
├── build
├── dist
├── examples
│   ├── __init__.py
│   ├── mypackage-example.py
│   ├── mypackage-simple-v1.py
│   ├── mypackage-simple-v2-report.py
│   └── mypackage-simple-v2.py
├── mypackage
│   ├── PKG-INFO
│   ├── __init__.py
│   ├── api_methods.py
│   ├── config.py
│   ├── connector.py
│   ├── contrib.py
│   ├── examples
│   │   ├── __init__.py
│   │   ├── mypackage-example.py
│   │   ├── mypackage-simple-v1.py
│   │   ├── mypackage-simple-v2-report.py
│   │   └── mypackage-simple-v2.py
│   ├── qcache
│   │   ├── __init__.py
│   │   └── redis.conf
│   ├── mypackage.egg-info
│   │   ├── PKG-INFO
│   │   ├── PKG-INFO.bak
│   │   ├── PKG-INFO.bak-e
│   │   ├── SOURCES.txt
│   │   ├── dependency_links.txt
│   │   ├── requires.txt
│   │   └── top_level.txt
│   ├── settings.py
│   ├── setup.cfg
│   ├── tests
│   │   └── test_qualys_cache.py
│   ├── util.py
│   └── version.py
└── setup.py

where I want to have tests within mypackage.tests I am having issues with dependent relative imports on the qcache extension module. The module depends on (and must depend on) the parent module. I was attempting to bring in parent module requirements via relative imports as follows:

from .. import api_methods, connect

Which works from the top-level project path, but not from within the module itself. Am I doing this right for pypi standard unit tests? I'm really new to writing pypi eggs so any advice here is appreciated.

Right now I'm running my tests as follows (from the project, not package directory)

python -m unittest mypackage.tests.test_qualys_cache

I wound up using nose2 and setuptools with virtualenv and virtualenvwrapper. This solved the relative import issue for me across instances.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM