1

Hello i'm currently have a real_estate_spider.py(inside spiders folder) script that required to import from items.py which is located outside directory(tutorial)

here is my folder structure

\---tutorial
|   items.py
|   middlewares.py
|   pipelines.py
|   settings.py
|   __init__.py
|
+---spiders
|   |   real_estate_spider.py
|   |   __init__.py
|   |
|   \---__pycache__
|           real_estate_spider.cpython-36.pyc
|           __init__.cpython-36.pyc
|           不動産.cpython-36.pyc
|
\---__pycache__
        items.cpython-36.pyc
        middlewares.cpython-36.pyc
        pipelines.cpython-36.pyc
        settings.cpython-36.pyc
        __init__.cpython-36.pyc

Here is my real_estate_spider.py file which has the import:

import scrapy
from ..items import real_estateItem
from scrapy.utils.markup import remove_tags
from scrapy_splash import SplashRequest
from datetime import datetime as dt

How do i import real_estateItem class which located from items.py in my real_estate_spider.py ? Thank you

4
  • Do you get any error message when executing your script? Commented Aug 2, 2018 at 7:17
  • yes i do i get "File "real_estate_spider.py", line 2, in <module> from ..items import real_estateItem ValueError: attempted relative import beyond top-level package" Commented Aug 2, 2018 at 7:21
  • Ok, how do you execute the script? Do you run it directly (e.g. python real_estate_spyder.py? Commented Aug 2, 2018 at 11:29
  • Yes i run directly like that cause i'm gonna use it with docker-compose so it will execute it rightaway Commented Aug 3, 2018 at 1:56

2 Answers 2

6

did you try

from .. import items

or

import sys
sys.path.append('../')
import items

while in real_estate_spider.py

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

5 Comments

invalid syntax :<
still the same error as when i first try" attempted relative import beyond top-level package" :(
try checking your system path , if you're trying on jupyter network online, you can have these issues, your checking system path with import sys (then next line) sys.path
4 years later lol, but i think they were trying to write import <module_name> directly, whereas the answer meant to change <module name> with items
Yeah <module_name> part should actually be items
1

Try appending the parrent dir to python

Ex: real_estate_spider.py

import sys
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(dir_path, os.pardir)))

import scrapy
from items import real_estateItem
from scrapy.utils.markup import remove_tags
from scrapy_splash import SplashRequest
from datetime import datetime as dt

7 Comments

but im trying to upload this script to docker which will run on other machines, will it affect if i try to docker run on other machines .Where should i put the path file in ? im still new to python and dont know about the sys and os module
I am not sure if this will cause problem with docker. I don't have one to test it right now..
im still confused on what to put in on the file, dir_patch, os.pardir
You have to just execute the above snippet as it is.....you do not need to modify anything
oh ok. it returned with "unable to import 'items'"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.