Discussion:
[Tutor] [newbie] import error after restart (virtualenv)
David
2015-09-19 13:22:28 UTC
Permalink
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)***@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)***@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


-- You received this message because you are subscribed to the Google
Groups "Django users" group. To unsubscribe from this group and stop
receiving emails from it, send an email to
django-users+***@googlegroups.com. To post to this group, send
email to django-***@googlegroups.com. Visit this group at
http://groups.google.com/group/django-users. To view this discussion on
the web visit
https://groups.google.com/d/msgid/django-users/55FCA478.5000609%40gmx.net.
For more options, visit https://groups.google.com/d/optout.


views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title></html>')


tests.py

from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest

from lists.views import home_page

class HomePageTest(TestCase):

def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.func, home_page)

def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
self.assertTrue(response.content.startswith(b'<html>'))
self.assertIn(b'<title>To-Do lists</title>', response.content)
self.assertTrue(response.content.endswith(b'</html>'))

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailm
Peter Otten
2015-09-19 14:07:53 UTC
Permalink
Post by David
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 --
python tests.py
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
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.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.
David
2015-09-19 16:21:42 UTC
Permalink
Hello Peter,

this was indeed the problem -- I didn't go through manage.py! Weird I
didn't have that on the radar anymore.

Putting lists/ onto the Python path did not solve the problem.

Thanks for your help!

David
Post by Peter Otten
Post by David
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 --
python tests.py
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
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.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https:
Danny Yoo
2015-09-19 16:27:12 UTC
Permalink
Post by David
Hello Peter,
this was indeed the problem -- I didn't go through manage.py! Weird I
didn't have that on the radar anymore.
Putting lists/ onto the Python path did not solve the problem.
You probably want to put the *parent* of lists/ onto the Python path.

Good luck!
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...