Note that there are some explanatory texts on larger screens.

plurals
  1. POScrapy: Importing a package from the project that's not in the same directory
    primarykey
    data
    text
    <p>I'm trying to import a package from my project which is not in the same directory as scrapy is in. The directory structure for my project is as follows:</p> <pre><code>Main __init__.py /XPaths __init.py XPaths.py /scrapper scrapy.cfg /scrapper __init.py settings.py items.py pipelines.py /spiders myspider.py </code></pre> <p>I'm trying to access <code>xpaths.py</code> from within <code>myspider.py</code>. Here are my attempts:</p> <p>1) <code>from Main.XPaths.XPaths import XPathsHandler</code></p> <p>2) <code>from XPaths.XPaths import XPathsHandler</code></p> <p>3) <code>from ..Xpaths.XPaths import XPathsHandler</code></p> <p>These failed with the error:</p> <pre><code>ImportError: No module named ....... </code></pre> <p>My last attempt was:</p> <p>4) <code>from ...Xpaths.XPaths import XPathsHandler</code></p> <p>Which also failed with the error:</p> <pre><code>ValueError: Attempted relative import beyond toplevel package </code></pre> <p>What am I doing wrong? <code>XPaths</code> is independent from Scrapy, therefore the file structure has to stay that way.</p> <p>//EDIT</p> <p>After some further debugging following @alecxe comment, I tried adding the path to <code>main</code> inside the <code>sys.path</code>, and print it before importing xpaths. The weird thing is, the <code>scrapper</code> directory gets appended to the path when I run scrapy. Here's what I added:</p> <pre><code>'C:\\Users\\LaptOmer\\Code\\Python\\PythonBackend\\Main' </code></pre> <p>And here's what I get when I print <code>sys.path</code>:</p> <pre><code>'C:\\Users\\LaptOmer\\Code\\Python\\PythonBackend\\Main\\scrapper' </code></pre> <p>Why does scrapy append that to the path?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload