Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

python how to run script in folder

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

Upvotes: 0

Views: 1029

Answers (2)

Sudeep Juvekar
Sudeep Juvekar

Reputation: 5068

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)

Upvotes: 1

DoHe
DoHe

Reputation: 73

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.

Upvotes: 0

Related Questions