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.

Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286

1 Answers1

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.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • thanks! I tried to use 2to3 tool to update the syntax but unfortunately got the same error in Python 3.2 – Niko Gamulin Jun 17 '12 at 11:28
  • @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. – Lev Levitsky Jun 17 '12 at 11:41
  • No, I just posted the output here http://stackoverflow.com/questions/11071037/how-to-use-2to3-tool-in-windows – Niko Gamulin Jun 17 '12 at 11:43
  • @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`. – Lev Levitsky Jun 17 '12 at 11:59
  • I already converted all files but now I get an error File "C:\Python32\modules\py2neo\batch.py", line 29, in from . import rest, neo4j ImportError: cannot import name neo4j – Niko Gamulin Jun 17 '12 at 12:02
  • 1
    @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. – Lev Levitsky Jun 17 '12 at 12:15