5

I am completely new to Python and wanted to use py2neo and tornado module.

In order to do that I ran setup.py for both modules and placed them into folders

C:\Python32\modules\py2neo

and

C:\Python32\modules\tornado

In the main program I guess these lines tell the interpreter where to look for files:

import sys
sys.path.append(r'C:\Python32\modules')
# Import Neo4j modules
from py2neo import neo4j, cypher

Reading the book I also added environmental variable (in Windows 7)

PYTHONPATH = C:\Python32\modules;C:\Python32\modules\tornado;C:\Python32\modules\py2neo

Edit

Now I figured out that Python Shell has to be restarted in order to load modified PYTHONPATH variable In case the variable value is PYTHONPATH = C:\Python32\modules and the program contains the line

from py2neo import neo4j, cypher

then the following lines are useless:

import sys
sys.path.append(r'C:\Python32\modules')

When I run the program however I get the following error:

Traceback (most recent call last):
 File "C:\...\Python Projects\HelloPython\HelloPython\Hellopy2neo.py", line 15, in <module>
 from py2neo import neo4j, cypher
 File "C:\Python32\modules\py2neo\neo4j.py", line 38, in <module>
 import rest, batch, cypher
ImportError: No module named rest

In the file neo4j.py there are the following lines:

try:
 import json
except ImportError:
 import simplejson as json
try:
 from urllib.parse import quote
except ImportError:
 from urllib import quote
try:
 from . import rest, batch, cypher
except ImportError:
 import rest, batch, cypher #line38

and rest.py file is located in folder C:\Python32\modules\py2neo so I don't know why I get the error

ImportError: No module named rest

Edit2:

Trying to import the py2neo directoy in Python Shell and list modules I get:

>>> import py2neo
>>> [name for name in dir(py2neo) if name[0] != '_']
['rest']

I guess there are some unneccesary imports as well and would be very thankful if anyone explained, which imports should be added and excluded (in PYTHONPATH and scripts) in order the program to run without errors.

asked Jun 17, 2012 at 8:22
3
  • Does C:\Python32\modules\py2neo have a file named __init__.py in it? Commented Jun 17, 2012 at 9:00
  • Does the rest module directory exist, and is it in the appropriate place? Commented Jun 17, 2012 at 9:31
  • @NikoGamulin When I run py2neo's setup.py with py3, I get an error. How did you get through that? Commented Jun 17, 2012 at 12:30

1 Answer 1

1

I suspect the problem is that the import syntax for relative imports has changed in transition from Python 2 to Python 3:

The only acceptable syntax for relative imports is from .[module] import name. All import forms not starting with . are interpreted as absolute imports.

The modules you installed use the syntax that would work in Python 2. You could either install them for Python 2, or look for a version of py2neo that supports Python 3, or try to port it manually (the import line should look like from . import rest, but you'll probably face other problems later) or with 2to3 tool.

Update: I tried installing py2neo with pip. It failed for Python3 and finished successfully for Python 2. The version is 1.2.14.

answered Jun 17, 2012 at 9:28
Sign up to request clarification or add additional context in comments.

6 Comments

thanks! I tried to use 2to3 tool to update the syntax but unfortunately got the same error in Python 3.2
@NikoGamulin Did it change the import statement? When I was porting my own module to Python3 with 2to3, it changed all local imports to the new syntax.
No, I just posted the output here stackoverflow.com/questions/11071037/…
@NikoGamulin The answer from there is right, you need -w to apply the changes. The output you see is what will be written with the -w flag, you see that imports are changed. Let us know if it works after you run 2to3 with -w.
@NikoGamulin Ah.. I'm sorry then, I guess the problem wasn't the syntax. The code you posted in the question does the correct py3-style import inside the try block. It then falls back to oldstyle import due to an ImportError. I think the developers intended that this would happen on old versions of Python, but in your case the error occurs for some other reason. Then it tries the old syntax and fails obviously. With 2to3 you forced it to use the new syntax but the problem, whatever it is, didn't go away.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.