[Tutor] [newbie] import error after restart (virtualenv)
Peter Otten
__peter__ at web.de
Sat Sep 19 16:07:53 CEST 2015
David wrote:
> Dear Tutors,
>
> I am reading through Harry Percival's "Test-Driven Development with
> Python".
>
> As I finished chapter 3 yesterday, I was fully on track, perfectly
> aligned with the book.
>
> Today I restarted my computer, activated the virtualenv in question --
> and get an error message that was not there beforehand:
>
>
(Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists/lists$
> python tests.py
> Traceback (most recent call last):
> File "tests.py", line 5, in <module>
> from lists.views import home_page
> ImportError: No module named 'lists'
>
>
> I neither understand why he doesn't find 'lists' anymore nor do I know
> how to solve the problem. Nothing seems to have changed in the meantime...
>
> Can you please guide me towards a solution?
>
> Thank you!
>
> David
>
>
>
> The project structure looks as follows:
>
> (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists$
> tree .
> ├── db.sqlite3
> ├── functional_tests.py
> ├── lists
> │ ├── admin.py
> │ ├── __init__.py
> │ ├── migrations
> │ │ └── __init__.py
> │ ├── models.py
> │ ├── tests.py
> │ └── views.py
> ├── manage.py
> └── superlists
> ├── __init__.py
> ├── __pycache__
> │ ├── __init__.cpython-34.pyc
> │ ├── settings.cpython-34.pyc
> │ ├── urls.cpython-34.pyc
> │ └── wsgi.cpython-34.pyc
> ├── settings.py
> ├── urls.py
> └── wsgi.py
Given this layout you have to ensure that the parent folder of lists is in
sys.path. This can be achieved by setting the PYTHONPATH variable for just
this invocation
$ PYTHONPATH=.. python tests.py
or in a more permanent way and preferably with absolute paths.
However, are you sure you ran tests.py explicitly? I've only had a cursory
look at django and no project handy to check, but if I remember correctly
$ ./manage.py test
should take care of the details.
More information about the Tutor
mailing list