0

This is my python path:

PYTHONPATH = D:\PythonPath

in the PythonPath folder I have MyTests folder that has a Script.py

in the PyThonPath folder I have ScrapyingProject folder

inside the Script.py I do this:

from ScrapyingProject.ScrapyingProject.spiders.XXXSpider import XXXSpider

I got this exception:

ImportError: No module named ScrapyingProjectScrapyingProject.spiders.XXXSpider

Edit:

the XXXSpider is in this location:

D:\PythonPath\ScrapyingProject2\ScrapyingProject2\spiders.py
2
  • Either put the file in the same directory or add the path to the file to your class-path Commented Feb 9, 2014 at 18:59
  • i assume spiders is a spider.py file. Move the file or, wouldn't an implementetion of if __name__ == "__main__": main() help out here? Commented Feb 9, 2014 at 19:12

2 Answers 2

1

Take a look at this to read more about Python modules and packages: http://docs.python.org/2/tutorial/modules.html

Turn your python-script-containing folder into a python package by adding __init__.py file to it. So, in your case, the directory structure should resemble this:

PYTHONPATH
- ScrapyingProject
   - __init__.py
   - script.py

Now, in this scheme, ScrappyProject becomes your python-package. Any .py file inside the folder becomes a python module. You can import a python module by dot-expanded python path starting PYTHONPATH. Something like,

from ScrapyingProject.script import XXXSpider

Same logic can be extended by nesting multiple packages inside each other. A nested package, for example looks like

PYTHONPATH
- ScrapyingProject2
   - __init__.py
   - ScrapyingProject2
       - __init__.py
       - script.py

Now, a package-nested script.py can be imported as

from ScrapyingProject2.ScrapyingProject2 import script

Or even

from ScrapyingProject2.ScrapyingProject2.script import XXXSpider

(Assuming you have defined class XXXSpider inside script.py)

Sign up to request clarification or add additional context in comments.

Comments

0

For one you say that the directory is D:\PythonPath\ScrapyingProject2\ScrapyingProject2\spiders.py but you import from ScrapyingProject.ScrapyingProject (without the 2s). If I understand you correctly, the wanted import should look something like this:

from ScrapyingProject2.ScrapyingProject2.spiders import XXXSpider

assuming the class XXXSpider is in the module spiders.py. Remember to put __init__.py files in the folders you want to import from (turning them into 'packages'). This should include all folders from the PYTHONPATH to the one containing the *.py-files. See here from more details on packages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.