I have a virtualenv that uses Python 3 and am currently using pytest for testing. I've packaged a program called "process_expiring_passwords" and import this package from my general script file called "pep.py". I've also used pip in my virtualenv to install my process_expiring_passwords package.
When I go to run my tests, however, I get the following error:
ImportError while importing test module '/home/username/projects/process-expiring-passwords/tests/test_process_expiring_passwords.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback: tests/test_process_expiring_passwords.py:3: in from process_expiring_passwords import ProcessExpiringPasswords process_expiring_passwords/process_expiring_passwords.py:16: in from process_expiring_passwords.models.ProcessExpiringPasswordsLog
\ E ModuleNotFoundError: No module named 'process_expiring_passwords.models'; 'process_expiring_passwords' is not a package
Is there a particular reason why I can use my package from my pep.py general script file while pytest is unable to find said package?
The essential parts of the code related to this issue are as follows:
pep.py:
from process_expiring_passwords.process_expiring_passwords \
import ProcessExpiringPasswords
pep = ProcessExpiringPasswords()
pep.process()
process_expiring_passwords/process_expiring_passwords.py:
from process_expiring_passwords.models.ProcessExpiringPasswordsLog \
import ProcessExpiringPasswordsLog
class ProcessExpiringPasswords:
def __init__(self):
self.example = 0
def process():
pepl = ProcessExpiringPasswordsLog()
process_expiring_passwords/models/ProcessExpiringPasswordsLog.py:
class ProcessExpiringPasswordsLog():
def __init__(self):
self.example = 0
process_expiring_passwords/setup.py:
# ref: https://github.com/pypa/sampleproject/blob/master/setup.py
# ref: http://python-packaging.readthedocs.io/en/latest/minimal.html
from setuptools import setup
setup(
name='process_expiring_passwords',
version='0.0.1',
packages=find_packages(),
description='Process expiring passwords',
license='MIT',
install_requires=[
'apiclient',
'google-api-python-client',
'httplib2',
'mysqlclient',
'oauth2client',
'requests',
'SQLAlchemy',
],
)
tests/test_process_expiring_passwords.py:
from process_expiring_passwords import ProcessExpiringPasswords
def test_pep():
pep = ProcessExpiringPasswords()
assert pep is not None
Project structure:
process_expiring_passwords
pep.py
process_expiring_passwords
__init__.py
models
__init__.py
ProcessExpiringPasswordsLog.py
process_expiring_passwords.py
setup.py
tests
test_process_expiring_passwords.py
Notes:
- Updated code example and setup.py based on feedback from Matt Messersmith.
pip -e .
the only reason I can think of is thatpytest
is not installed in your virtualenv and thus you run the "main python" variant which can not find your module in the PATH. Install pytest in your virtualenv and it should work. – MrLeehimport process_expiring_passwords
insideprocess_expiring_passwords/process_expiring_passwords.py
, you don't import the parent package. Add linesimport process_expiring_passwords; print(process_expiring_passwords.__file__)
toprocess_expiring_passwords.py
and verify that you import the module instead. – hoeflingprocess_expiring_passwords
which it presumably should collect and install, how do you pass the package topackages
list? Do you usesetuptools.find_packages
for that? The same naming of the package and module may be only a complication that distracts from the actual error. – hoefling