python : ImportError: No module named utils with clean_string - python

Note: I have already check question with same error as mine but mine is different i want to ask if "clean_string, clean_number, clean_text, clean_float, clean_int"
agency_id = scrapy.Field(serializer=clean_string)
are some in built function in python or i have to import to make it work
I am new in python just doing some programming stuffs.
Below i my code snippet
from .utils import clean_string, clean_number, clean_text, clean_float, clean_int
from six.moves.urllib.parse import urljoin
class MyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
agency_id = scrapy.Field(serializer=clean_string)
when i run above code it give me error
**ImportError: No module named utils**
can you help with it have i to install clean_string or something

As per our discussion, please install python -m pip install pyes
do it as below:
from pyes import utils
# use it like below
class MyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
agency_id = scrapy.Field(serializer=utils.clean_string)
(or)
from pyes.utils import clean_string
# use it like below
class MyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
agency_id = scrapy.Field(serializer=clean_string)
you cannot use from .utils import clean_string because it looks for utils in current working directory. Instead either use from django import utils (or) use from pyes.utils import clean_string

Related

cannot import the module in Python

I have the following folder structure.
check_site
- test_site
-- views.py
- app2
- app3
- modules
-- url.py
-- usability.py
module ulr.py contains one class inside - Url.py
class URL:
...
module usability.py contains one class that inherit URL class
from url import URL
class Usability(URL):
...
And then I have a view.py where I neen to import class Usability
from modules.url import URL
from modules.usability import Usability
And here is a problem. It gives me an error
from url import URL
ModuleNotFoundError: No module named 'url'
I've tried to change the import in usability.py to
from modules.url import URL but in this case it gives the error in the usability.py
Unable to import modules.url
I've also tried
from .url import URL and from check_site.modules.url import URL But these also don't work
If someone knows how to fix it, please help
Well, the problem lies here because by default Python searches for the file in the current directory but the file u want to import is not in the same directory as your program.
You should try sys.path
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
This should work in most cases.

Import constant from external file into python

I would like to import a constant from an external file. I have two files in one directory.
constants.py file:
SOME_CONSTANT = 'something'
And import this into settings.py
import constants
someVariable = constants.SOME_CONSTANT
But pylint write that Module 'constants' has no 'SOME_CONSTANT' member
Can't really tell how you made your constants, but ideally you'd want to store them in your class.
#Constants.Py
class Province:
CITY = 'Toronto'
#Settings.Py
from Constants import Province
someVariable = Province.CITY
>>> 'Toronto'

ModuleNotFoundError: No module named '' in Scrapy

(This is my items.py)
import scrapy
class FreelanceItem(scrapy.Item):
url = scrapy.Field()
url = scrapy.Field()
When I started another python and imported Package
import scrapy
from scrapy.item import Item , Field
from freelance.items import FreelanceItem
I get this :
ModuleNotFoundError: No module named 'freelance'
How should I do ?
thanks.
Youre accessing it the wrong way..
Lets say you are in a directory called PythonTest, where you also have your main.py file.
Steps:
Create a folder named "freelance" in this PythonTest Directory
add an empty file in this directory (freelance dir) named : "_ init _.py" (this tells python it is a package)
add your items.py file aswell in this directory
Now go to your 'main.py' and add the line:
from freelance.items import FreeLanceItem
Also make sure to have correct indenting in your code.(see below)
import scrapy
class FreeLanceItem(scrapy.Item):
url = scrapy.Field()
url = scrapy.Field()
running the code should not produce an error anymore.
Let me know if this helped!

Reference another class in python

I'm just starting out with Python for Google App Engine. I have a file notifications.py, and in here, I will be creating User entities, which are specified in users.py. How can I do this? I've tried import users, but I get an error: NameError: global name 'User' is not defined
Oh, I just had this problem too! After you do:
import users
to get User you have to type users.User
Alternatively you could import it like:
from users import User
then reference it as just User but if you do it this way you'll have to list every bit from users that you want in the following format:
from users import User, Somthingelse, Somthing
If you're feeling super lazy and you don't want to type in any prefixes or list all the things you want, just type
from users import *
Instead of
import users
do
from users import User
# module.py
foo = "bar"
# main.py
import module
print foo # This will cause error because foo is not located in the current namespace
print module.foo # this will print "bar"
from module import foo # But you can import contents of module "module" in the current namespace
http://docs.python.org/tutorial/modules.html

Django - importing dynamically

I'm trying to do a dynamic import of a python module in django. I have two different apps that I want to import from, and I want to replace these import statements:
from app1.forms import App1ProfileForm
from app2.forms import App2ProfileForm
I am dynamically able to create the strings App1ProfileForm and App2ProfileForm and then instantiate them like so:
globals()[form]()
I tried following some of the instructions in this post: Dynamically import class by name for static access
and so I tried doing this:
theModule = __import__("app1.forms.App1ProfileForm")
but I'm getting an error that says No module named App1ProfileForm
EDIT:::
Ok I tried this code:
theModule = __import__("app1")
print theModule
theClass = getattr(theModule,'forms')
print theClass
theForm = getattr(theClass,'App1ProfileForm')
print theForm
theForm.initialize()
but I get an error that type object 'App1ProfileForm' has no attribute 'initialize'
You don't want to do this. Imports are done when the relevant code is first executed - in the case of module-level imports, it's when the module itself is imported. If you're depending on something in the request, or some other run-time element, to determine what class you want, then this will not work.
Instead, just import them both, and get the code to choose which one you need:
from app1.forms import App1ProfileForm
from app2.forms import App2ProfileForm
forms = {'app1': App1ProfileForm,
'app2': App2ProfileForm}
relevant_form = forms[whatever_the_dependent_value_is]
I don't quite know how you're generting the string to import. I'll assume you generate the whole "path". Try this:
def import_from_strings(paths):
ret = []
for path in paths:
module_name, class_name = path.rsplit('.', 1)
module = __import__(module_name, globals(), locals(), [class_name], -1)
ret.append(getattr(module, class_name))
return ret
Aren't you trying to import a class, and not a module ? I'm not an expert, but I think you must import the module using __import__, then select it's App1ProfileForm class with something like yourmodule.App1ProfileForm
I figured it out. Here's how to do it:
theModule = __import__(module_name+".forms") # for some reason need the .forms part
theClass = getattr(theModule,'forms')
theForm = getattr(theClass,form_name)
then to initialize:
theForm() or theForm(request.POST)

Categories