0
spiders/
ekantipur.py
    class ekantipurSpider(XMLFeedSpider):

tests/
   responses
      __init__.py
      spiderdir
        Archive_ekantipur.html
   spiderdir_test.py
   from spiders import ekantipur 

In the given file structure, when I try to access the class ekantipurSpider in spiderdir_test.py,i.e from spiders import ekantipur, it says following error.

from spiders import ekantipur
ImportError: No module named spiders
Ramesh KC
  • 570
  • 7
  • 30

2 Answers2

0

You cannot import spiders from the tests directory like that, unless you have it in your PYTHON_PATH environment variable. Please read about Python's import system.

For a quick and dirty (!) fix add this at the top of your file:

import sys
sys.path.append("..")

Or read this post about relative imports: Import a module from a relative path

Community
  • 1
  • 1
Fabian
  • 4,160
  • 20
  • 32
0

try running the spiderdir_test.py from the parent folder of spiders and tests with this command "python -m tests/spiderdir_test". Note that you need to have __init__.py into tests folder also. Basically you will be running spiderdir_test.py as a module.

Gunjan
  • 2,775
  • 27
  • 30
  • (news-scraper)➜ crawler git:(development) ✗ python -m tests/spiderdir_test.py /home/ramesh/.virtualenvs/news-scraper/bin/python: Import by filename is not supported. This is the error I got when try to run from shell. – Ramesh KC Jan 19 '15 at 10:07
  • use "spiderdir_test" instead of "spiderdir_test.py" – Gunjan Jan 19 '15 at 10:09
  • python -m tests/spiderdir_test Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/var/www/news-scrapper/src/crawler/tests/spiderdir_test.py", line 3, in from spiders.ekantipur import ekantipur File "spiders/ekantipur.py", line 5, in from crawler.items import NewsItem ImportError: No module named crawler.items It effects on from crawler.items import NewsItem import. – Ramesh KC Jan 19 '15 at 10:25
  • running the module from parent folder will need all the import statement made according to the parent folder. So the crawlers folder should be parallel to the tests and parallel folder. or else try like this "from tests.crawlers.items import NewsItem", (provides crawlers is inside tests folder), according to your folder structure. – Gunjan Jan 19 '15 at 10:55