Im at a mental block of how I can import my python class currently saved as Scrapese.py.
import scrapy
class Scrapese(scrapy.Spider):
name = 'scrape-se'
seach_engine = [
'se1.com',
'se2.com',
]
def parse(self, seach_engine, site_to_parse, page_start, page_end, response):
site = str(seach_engine+site_to_parse)
if site_to_parse == seach_engine[0]:
print("executing against se1!")
elif site_to_parse == searh_engine[1]:
print("executing against se2!")
else:
print("Something bad happened.")
I keep trying to typical:
from Scrapese import parse
but it says:
ImportError: cannot import name 'parse'
What am I doing wrong?
Thanks
Scrapese is the name of a Python module in which you define a class also called Scrapese.
The line from Scrapese import parse will cause the Python interpreter to try importing a module called Scrapese and looking in that for an object parse.
What you probably want to do is the following:
# Scrapese.py
class Scrapese(object):
def parse(self):
pass
# main.py
from Scrapese import Scrapese
o = Scrapese()
o.parse()
This will cause the Python interpreter to make the Scrapese class definition available within another script (main.py), which you can then instantiate and use for parsing.
Related
I am using unittest module for writing tests.
I need to test initialization of the object inside a testcase using different inputs.
For this purpose I am importing the class inside setUp(). But when I try to use the class inside test_*() functions, I get this error - NameError: name 'Example' is not defined
Here is my code sample-
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
import Example
def test_sample_function(self):
e = Example(1,2)
I know that I can simply import the class at top of the script. But I do not want to do that. I need to import it only during setup of the testscript.
Looking for some help here.
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
import Example
self.Example = Example
def test_sample_function(self):
e = self.Example(1,2)
There's no reason to import the module in setUp. The module is still available globally in sys.modules, but you've only bound it to a local name that goes away after setUp returns. Just import it globally.
import unittest
import Example
class TestExample(unittest.TestCase):
def test_sample_function(self):
e = Example(1,2)
Here is my directory structure:
In file keyword.py I import lottery.lottery at the first line like this:
from lottery.lotterya import Lottery
In file rule.py I import lottery.keyword dynamically like this:
__import('lottery.keyword') but it reports an error "No module named lotterya".
I don't know what to do. Can anyone help?
I dynamically import a module
Here is one solution for your question. It uses importlib to do dynamic import.
In ruly.py
import importlib
if __name__ == '__main__':
mKey = importlib.import_module('lottery.keyword')
MyKeyword = getattr(mKey,'MyKeyword')
k = MyKeyword()
k.mPrint()
In keyword.py
from lottery.lotterya import Lotterya
class MyKeyword():
def __init__(self):
pass
def mPrint(self):
print 'Hello, keyword'
l = Lotterya()
l.lPrint()
In lotterya.py
class Lotterya:
def __init__(self):
pass
def lPrint(self):
print 'Hello, Lotterya'
I've read this good text as a reference, but I still can't solve my circular problem:
import pygame
import python
import background
import player
import parser
class Game():
map1 = parser.Parser("map1")
map1.parse()
The parser.py module:
import os, sys
def replaceExtension(mapPath):
# content
class Parser():
def __init__(self, map, path="Maps/"):
replaceExtension(path)
# content
When I run my main file:
map1 = parser.Parser("map1")
AttributeError: 'module' object has no attribute 'Parser'
For some obscure reason it just does not find my Parser class.
There is a built-in module called parser.
That's the one that gets imported. You need to rename your module.
You can find more on import order here: http://docs.python.org/2/tutorial/modules.html#the-module-search-path
My (simplified) project layout is as follows:
/__init__.py
/test.py
/lib/__init__.py
/lib/client.py
my test.py is simply:
import lib.client
A = client()
A.Test()
and my lib\client.py begins as follows:
import ui #(another class in the lib dir)
class client(object):
"""
(Blah)
"""
UI = None
def __init__():
UI = ui()
def Test():
print "Success"
When I attempt to run test.py, I can step into the code and see that the definitions in client are parsed, however, when I get to the line where I instantiate a client, I get the following exception:
NameError: name 'client' is not defined
if I change that line to be:
A = lib.client()
Then I get
'module' object is not callable
What am I missing?
the lib.client object you have after import lib.client is the module, not the class. To instantiate the class you need to call the class in the module object:
A = lib.client.client()
or, as #rantanplan said, import the class from the module
from lib.client import client
A = client()
I just understood that you do the imports the Java way.
In python when you do :
import lib.client
You don't make available all the definitions in that module. You just
made available the actual module - the client.py
So either you keep the import scheme as you have now and do
import lib.client
A = lib.client.client()
or
from lib.client import client
A = client()
Also I suggest you name your python classes with capitalized camelcase
i.e.
class Client(object):
As it is the python convention.
Either it's lack of sleep but I feel silly that I can't get this. I have a plugin, I see it get loaded but I can't instantiate it in my main file:
from transformers.FOMIBaseClass import find_plugins, register
find_plugins()
Here's my FOMIBaseClass:
from PluginBase import MountPoint
import sys
import os
class FOMIBaseClass(object):
__metaclass__ = MountPoint
def __init__(self):
pass
def init_plugins(self):
pass
def find_plugins():
plugin_dir = os.path.dirname(os.path.realpath(__file__))
plugin_files = [x[:-3] for x in os.listdir(plugin_dir) if x.endswith("Transformer.py")]
sys.path.insert(0, plugin_dir)
for plugin in plugin_files:
mod = __import__(plugin)
Here's my MountPoint:
class MountPoint(type):
def __init__(cls,name,bases,attrs):
if not hasattr(cls,'plugins'):
cls.plugins = []
else:
cls.plugins.append(cls)
I see it being loaded:
# /Users/carlos/Desktop/ws_working_folder/python/transformers/SctyDistTransformer.pyc matches /Users/carlos/Desktop/ws_working_folder/python/transformers/SctyDistTransformer.py
import SctyDistTransformer # precompiled from /Users/carlos/Desktop/ws_working_folder/python/transformers/SctyDistTransformer.pyc
But, for the life of me, I can't instantiate the 'SctyDistTransformer' module from the main file. I know I'm missing something trivial. Basically, I want to employ a class loading plugin.
To dymically load Python modules from arbitrary folders use imp module:
http://docs.python.org/library/imp.html
Specifically the code should look like:
mod = imp.load_source("MyModule", "MyModule.py")
clz = getattr(mod, "MyClassName")
Also if you are building serious plug-in architecture I recommend using Python eggs and entry points:
http://wiki.pylonshq.com/display/pylonscookbook/Using+Entry+Points+to+Write+Plugins
https://github.com/miohtama/vvv/blob/master/vvv/main.py#L104