Import Error: No module named spiders - python

I have the following directory tree and I'm trying to import models in my_spider.py:
.
├── my_spider
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── items.py
│   ├── pipelines.py
│   ├── settings.py
│   ├── settings.pyc
│   └── spiders
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── my_spider.py
│   ├── my_spider.pyc
│   └── models.py
└── scrapy.cfg
In my_spider.py, I have:
from my_spider.spiders import models
I get an error saying ImportError: No module named spiders. I must be doing something very basic wrong.
Any help will be appreciated.
Thanks

Since my_spider.py and models.py are in the same directory, I think you should just use import models

Related

How to import locally in a proper way in PyCharm OSX

Here's how the directory looks like
├── Caches
│   ├── 11\ Centroids\ representing\ relative\ anchor\ sizes..png
│   ├── 9\ Centroids\ representing\ relative\ anchor\ sizes..png
│   ├── Generated\ anchors\ relative\ to\ sample\ image\ size.png
│   ├── Relative\ width\ and\ height\ for\ 10107\ boxes..png
│   └── data_set_labels.csv
├── Config
│   ├── feature_map.py
│   ├── set_annotation_conf.py
│   └── voc_conf.json
├── Helpers
│   ├── __pycache__
│   │   ├── annotation_parsers.cpython-37.pyc
│   │   └── visual_tools.cpython-37.pyc
│   ├── anchors.py
│   ├── annotation_parsers.py
│   ├── dataset_handlers.py
│   ├── models.py
│   └── visual_tools.py
├── README.md
├── sample_img.png
└── structure_requirements.txt
in dataset_handlers.py I need to import the following:
from ..Config.feature_map import get_feature_map
and in anchors.py I need to import the following:
from .visual_tools import visualization_wrapper
Both imports do resolve in PyCharm or in other words do not have syntax errors however when are run they give the following error:
ImportError: attempted relative import with no known parent package
If I do this:
from visual_tools import visualization_wrapper
the name visual_tools is unresolved however it runs without errors and it imports the requested things
How to be able to make relative imports from .some_module import something without getting errors and being resolved without marking the directory as sources root.

Build a scrapy project usint setuptools

I have a Scrapy project that need to be build, below the project structure:
.
├── cli.py
├── docs
│   ├── README.md
│   └── README.md.html
├── __init__.py
├── my_scrapy_project
│   ├── exporters.py
│   ├── __init__.py
│   ├── items.py
│   ├── middlewares
│   │   ├── __init__.py
│   │   ├── randomproxy.py
│   │   └── random_user_agent.py
│   ├── middlewares.py
│   ├── pipelines.py
│   ├── settings.py
│   ├── signals.py
│   └── spiders
│   ├── website_extractor_1.py
│   ├── __init__.py
│   └── website_extractor_2.py
├── MANIFEST.in
├── requirements.txt
├── scrapy.cfg
├── setup.py
└── VERSION
Scrapy spiders are executed by cli.py, when I try to build this package it's doesn't inlcude the cli.py, if I put this file cli.py in scripts key in setup.py it doesn't found Scrapy settings when I use get_project_settings
Any help please?

Skip over levels of a directory when importing python package

I've got a directory structure like:
Folder_in_PYTHONPATH/
├── Package1
│   ├── __init__.py
│   ├── src
│   │   ├── Class1.py
│   │   ├── Class2.py
│   │   └── __init__.py
│   └── test
│   └── testfile.py
├── Package2
│   ├── __init__.py
│   ├── src
│   │   ├── Class1.py
│   │   ├── Class2.py
│   │   └── __init__.py
│   └── test
│   ├── test1.py
│   └── test2.py
.
.
.
When I import things from this folder, I need to always type
import Package1.src.Class1
Is there any way to set up my __init__.py so that I can just type
import Package1.Class1
instead?
Add them into your packages' __init__.py files so they look like:
from src import Class1
from src import Class2
Have a look at the docs
I would recommend putting the *.py files in the top level folder of their package to get the import Package_1.Class1 behaviour you are after. The unit tests can stay in their own folder to keep them separate.

Not able to run python file which is under django project

I have my project tree like .
├── sizer
│   ├── manage.py
│   ├── node
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── node_serializer.py
│   │   ├── node_serializer.pyc
│   │   ├── part_serializer.py
│   │   ├── part_serializer.pyc
│   │   ├── Part_Serializer.pyc
│   │   ├── test.py
│   │   ├── test.pyc
│   │   ├── tests.py
│   │   ├── tests.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── requirement.txt
│   ├── sizer
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── wsgi.py
│   │   └── wsgi.pyc
│   ├── solver
│   │   ├── attrib.py
│   │   ├── attrib.pyc
│   │   ├── cap.py
│   │   ├── cap.pyc
│   │   ├── __init__.py
│   │   ├── node.py
│   │   ├── node.pyc
│   │   ├── nodes1.json
│   │   ├── nodes2.json
│   │   ├── parts.json
│   ├── strings.py
│   ├── strings.pyc
│   └── workload
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
I have created node and workload app by manage.py startapp command .
In the above directory structure I copied solver .Now I am importing my node.model under sizer.py file like .
import json
from pulp import *
from attrib import *
from cap import *
from node import *
from wl import *
from sizer.node.models import Part,Node
When I run python solver/sizer.py I am keep on getting
ImportError: No module named node.models
Please help me out what might I am doing wrong here .Spent more then 4 hours still not able to figure out .
Thanks
If your app name is node then your import statement should look like:
from node.models import Part, Node
Note that this requires that you already included node in the INSTALLED_APPS in your settings.py.
There are multiple reasons why an import might fail.
The module is not on the path. To check this print sys.path in your script just before you import the module.
The module is broken and cannot be imported. You can check this by opening a Python console in the same directory as the module and attempting to import. Does that work?
Importing the module results in a CIRCULAR import. This means that the import imports another module that imports another module that imports the original module. This is easy enough to avoid with a little thought and a clear hierarchy.
So, which problem do you have? I have no idea because I can't see the sys.path, and I can't see the code in your files.
What I can see is a bit of a mess. You have multiple modules named 'node'. You have manage.py files at multiple levels. You've included the .pyc files in the output instead of editing them out for the reader. You have so many different modules called 'node', 'sizer' or 'solver' that it must be VERY confusing to figure out which one is being imported at any given time.
Your underlying problem might be that you are trying to work on a project without using source control (git) which means you don't know what changes broke things and you don't feel brave about making big changes because you have no way of stepping back in time if they don't work out.

How to fix "ImportError: No module named ..."

I have reviewed most of the similar question here.
I'm new to python and I'm using Ubuntu 13.10
The project structure is
├── projecttest
│   ├── api.py
│   ├── controller
│   │   ├── controller.py
│   │   ├── controller.pyc
│   │   ├── init_db.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   └── extra
│   │   ├── extra.py
│   │   ├── extra.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── __init__.py
│   ├── lib
│   │   └── __init__.py
│   ├── models
│   │   ├── documents.py
│   │   ├── documents.pyc
│   │   └── __init__.py
All the __init__.py files are empty (no hidden characters) and when I'm trying
$ python init_db.py
that has:
from projecttest.models.documents import *
I'm getting:
Traceback (most recent call last):
File "controllers/init_db.py", line 1, in <module>
from projecttest.models.documents import *
ImportError: No module named projecttest.models.documents
You need to specify PYTHONPATH environment variable, it augments the default search path for module files.
It helps to think about PYTHONPATH as an absolute path. If you specify it you may import modules within your program relative to PYTHONPATH.
In your case it would be something like the following line:
PYTHONPATH=/<dir>/<folder>/projecttest/ python init_db.py
Then you may import modules without problems like:
from models.documents import *

Categories