Scrapy: No module named test_1.items - python-2.7

Im trying from import items from the .items file with from test_1.items import Items i keep getting the "No module named test_1.items" error. My structure is
test_1/
test_1/test_2/
test_1/scrapy.cfg
test_1/test_2/spiders/
test_1/test_2/ __init__.py
test_1/test_2/items.py
test_1/test_2/pipelines.py
test_1/test_2/settings.py
test_1/test_2/spiders/ __init__.py
test_1/test_2/spiders/today_spider.py
im coding in today_spider after i modify the items.py file and getImportError: No module named... this error. As you see i tried to change the manes not to be identical. i also tryied to start today_spider with __future__ import absolute_import. any advise?
thks a lot for your time

Related

ImportError: attempted relative import beyond top-level package whilst referring within same app

I am here because google search didn't solve the issue yet.
Django Version 4.0.6
Project structure created in Visual Studio Community 2022 is like as follows,
Project0
|
--Project0
|
---settings.py
--- <>
|
--App1
|
---models.py
---views.py
---forms.py
|
--App2
|
---models.py
---views.py
---forms.py
---tables.py
Project0, App1, App2 are all in same hierarchy.
When trying to run the solution, I am getting the following errors with App2 files,
File "D:\Projects\Django\Project0\App2\urls.py", line 2, in <module>
from . import views
File "D:\Projects\Django\Project0\App2\views.py", line 5, in <module>
from .tables import ProductTable, ProductHTMXtable
File "D:\Projects\Django\Project0\App2\tables.py", line 2, in <module>
from ..App1.models import List
ImportError: attempted relative import beyond top-level package
1st issue:
Here in App2 there are no models created. Instead, importing models from App1's model. So I guess the line --> from ..App1.models import List <-- might be wrong. If I remove those 2 dots '..' then I get Import could not be resolved error and the models are not being referred though its not stopping the solution from running.
2nd issue:
Why do --> from . import views <-- & --> from .tables import <-- too are throwing errors.
Unable to fix theses issues. Please help.
Answering to Balizok: If I do so I am getting not resolved error as attached here.
App1Model could not be resolved error
Import using from App1.models import List instead of from ..App1.models import List and that should do it.
As it is now, you're going, as the error suggests, beyond top-level package by using the ..App1, where refering to the app name doesn't.

Django: Import from project root

I was searching for an answer but could not get the solution for this exact case:
So, my project structure is like this
project/
myproject/
...
app/
...
views.py
...
logic/
do_stuff.py
db.sqlite3
manage.py
And this do_stuff.py contains a function call_me()
How can I call call_me() function from views.py (For example, upon a successful file upload)?
Many thanks!
Update:
I'm able to import a do_stuff() function from a single logic.py file from project/ like this: from .logic import do_stuff
But the problem is that the logic package is already written and has its folder structure.
I just thought of installing this package via settings.py, is there any way to do so?
Normally from project.logic.do_stuff import call_me should work in views.py. Have you tried?

how can I import subfolders module python to another file?

I want to access to my modules.module function A in my main , but when I do this I have an error that I cannot import that.. how can I fix it? I Have seen multiples articles but i hadnt a chance, how can I fix this error of importing modules from subfolders?
**/tool
main.py
/google
/modules
__init__.py
module.py**
ImportError: cannot import name google
main.py
#!/usr/bin/python
import sys
import core.settings
from google.modules import google
if __name__ == '__main__':
try:
core.settings.settings()
google()
except KeyboardInterrupt:
print "interrupted by user.."
except:
sys.exit()
module.py
def google():
print 'A'
the easiest way to work this out is have your main.py in your highest directory (it makes sense anyway for main to be there) and if you really want the real main to be in a sub directory have a dummy main at the top level you can call that just calls the actual main, that way python can see your entire directory tree and will know how to import any sub directory.
alternatively
you could add your parent directory to the sys.path:
parent_dir = os.path.realpath(os.path.join(os.getcwd(),'..')))
sys.path.append(parent_dir)
which will add that directory to the places python searches for when you try to import stuff.
but then you will need to keep track of how deep your main is in the directory tree and its a pretty unelegant solution in my opinion

Python packaging can't import class handler

I have two packages (diretories ) in my Python project
src
/textmining
mining.py...def mining():#...
__init.py....__all__ = ["mining"]
/crawler
crawler.py
in crawler.py I use the mining class
mining=mining()
main.py
__init__.py
my main.py is as follow:
scrapy_command = 'scrapy runspider {spider_name} -a crawling_level="{param_1}"'.format(spider_name='crawler/crawler.py',
param_1=crawling_level)
process = subprocess.Popen(scrapy_command, shell=True)
when I run crawler, it prompts
runspider: error: Unable to load 'Crawler.py': cannot import name mining
You need an __init__.py in every folder that's a part of the same package module.
src
__init__.py
/textmining
__init__.py
mining.py
/crawler
__init__.py
crawler.py
For simplicity, you should add a main.py in the src folder and call the function you want to start your program with from there as it's fairly difficult to import modules from sibling directories if you start your script in a non-root directory.
main.py
from crawler import crawler
crawler.start_function()
crawler.py
from src.textmining import mining
miner = mining()
Without turning everything into a python module you'd have to import a folder into the current script or __init__.py module by adding to path:
# In crawler.py
import sys
import os
sys.path.append(os.path.abspath('../textmining'))
import mining
However, messing around with the path requires you to keep in mind what you've done and may not be a thing you desire.

From module import * is not importing my functions

I am trying to understand how to import code from one file to another. I have two files file1.py and file2.py. I am running code in the first file, and have many variables and functions defined in the second file. I am using from file2 import * to import the code into file1.py. I have no problem using variables defined in file2.py in file1.py, but with functions I am getting NameError: name 'myfunc' is not defined when I try to use the function in file1.py. I can fix this problem by writing from file2 import myfunc, but I thought writing * would import everything from that file. What is the difference for functions versus variables?
I have tried to recreate the setup you have described and it is working OK for me. Hopefully this will give you an idea of how to get it to work.
# file1.py #####################################
import sys
sys.path.append("/home/neko/test/")
import file2
if __name__ == "__main__":
file2.testfunc()
# file2.py ######################################
testvar = 'hello'
def testfunc(): print testvar
For this test I was using python version 2.6.6
Both file1.py and file2.py is in /home/neko/test/