English 中文(简体)
Module import path
原标题:

I m unable to test-run a cssparser that I d like to use.

test.py:

from css.parse import parse

data = """
    em {
    padding: 2px; 
    margin: 1em;
    border-width: medium;
    border-style: dashed;
    line-height: 2.4em;
    }
    p { color: red; font-size: 12pt }
    p:first-letter { color: green; font-size: 200% }
    p:first-line { color: blue }"""


for rule in parse(data):
    print (rule)

..gives an error:

Traceback (most recent call last):
  method <module> in test.py at line 1
    from css.parse import parse
  method <module> in test.py at line 6
    from . import css, csslex, cssyacc
  method <module> in test.py at line 8
    from . import serialize
  method <module> in test.py at line 6
    from . import css
ImportError: cannot import name css

Directory structure (/Users/nimbuz/Documents/python31):

/Users/nimbuz/Documents/python31/**csspy**/
|
+-- css/ (*has __init__.py*)
|
+-- uri/ (*has __init__.py*)
|
+-- test.py

print(sys.path) shows:

[ /Users/nimbuz/Documents/python31/csspy ,  /Library/Frameworks/Python.framework/Versions/3.1/lib/python31.zip ,  /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1 ,  /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/plat-darwin ,  /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/lib-dynload ,  /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages ]
最佳回答

Here is few steps I just tested.

  • readme says its python 2.5, so you need python 2.x series

  • I have created a folder C:/TEST/

  • I have downloaded all files from css-py svn to C:/TEST/, so C:/TEST/css/ and C:/TEST/uri/ folders exists now.

  • I have downloaded ply s tar gz file and extract only ply folder into C:/TEST/css/, so C:/TEST/css/ply/ folder exists now.

  • I have created test.py in C:/TEST/ with the content

    from css.parse import parse
    print dir(parse)
    
  • and I run it and the results is like this, without import errors:

    C:TEST>test.py

    [ __call__ , __class__ , __closure__ , __code__ , __defaults__ , __delattr__ , __dict__ , __doc__ , __format__ , __get__ , __getattribute__ , __globals__ , __hash__ , __init__ , __module__ , __name__ , __new__ , __reduce__ , __reduce_ex__ , __repr__ , __setattr__ , __sizeof__ , __str__ , __subclasshook__ , func_closure , func_code , func_defaults , func_dict , func_doc , func_globals , func_name ]

Hope this helps. sorry If my explanation is bad.

问题回答

Do you have __init__.py files in the cssparser and css directories, to turn the directories into packages? Is there a css/parse.py file with some function or class named parse in it? What s on your sys.path, and what s the current directory, when you execute test.py? All of these questions may be important, and you give us the answer to none of them, so it s hard to be of any specific help!-)

python 3 version of css-py

http://ifile.it/v32n70s/css.zip

Does your css/ have an __init__.py?

Don t call your own file css.py. Name it something else, like css_test.py. Delete the css.pyc file also.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签