python code works fine on python shell but not thr

2019-08-26 20:18发布

问题:

This question already has an answer here:

  • Why does this work in the Python IDLE shell but not when I run it as a Python script from the command prompt? 4 answers
  • Python csv import fails 2 answers

as said in the title I'm having problem runnig my code from cmd which is working just fine in python shell.

I have py2neo version 4.

here is my code:

from py2neo import Graph
print ("passed")

here is the output from python shell:

passed
>>> 

here is the output from cmd command:

D:\py2neo>python py2neo.py

Traceback (most recent call last):
  File "py2neo.py", line 1, in <module>
    from py2neo import Graph
  File "D:\ALI\Desktop\ayten\mine-py2neo\py2neo.py", line 1, in <module>
    from py2neo import Graph
ImportError: cannot import name 'Graph'

I've been searching, the answer out there were the different versions of python that I have on my system, which has cause this problem. But I checked that out and realized that both of them have the same version.

here is how i figured out the versions:

opening the python shell:

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> 

in cmd:

D:\py2neo>python --version
Python 3.6.4

D:\py2neo>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Thank you in advance.

回答1:

Are you positive that py2neo is installed? What happens if you run pip install py2neo? I've had issues with pip and python versions in the past, so I would make sure that your pip is the same version as your python by running pip --version