3
import numpy as np
import matplotlib.pyplot as plt

def main():
    x = np.arange(0, 5, 0.1)
    y = np.sin(x)
    plt.plot(x, y)

if __name__ == '__main__':
    main()

Traceback (most recent call last):

  File 
"/Users/tim/workspace/Python/MachineLearn/test.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 63, in pylab_setup
    [backend_name], 0)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 11, in <module>
    from datalore.display import display
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_display/datalore/display/__init__.py", line 1, in <module>
    from .display_ import *
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_display/datalore/display/display_.py", line 5, in <module>
    from urllib.parse import urlencode
ImportError: No module named parse

Process finished with exit code 1

=================

Python: 2.7.16

PyCharm Professional: 2019.2

=================

btw, the code run in console mode is work

5
  • where is from urlparse import urlparse ? Commented Jul 30, 2019 at 8:14
  • probably you have multiple python versions on your machine and pycharm uses a different one. There is python2 and python3 and the urllib module is different in both. pycharm lets you select the python version of a project in the settings, probably changing the value there will fix your problem, as pycharm will default to using python3 nowadays Commented Jul 30, 2019 at 8:14
  • I have added an answer, hope it will help you Commented Jul 30, 2019 at 8:16
  • @576i but I have try edit code and write "import sys", "print sys.version", it shows "2.7.16" Commented Jul 30, 2019 at 8:20
  • @GOVINDDIXIT that's all code there, its a simply code, I have not import any urlparse library. Commented Jul 30, 2019 at 8:21

3 Answers 3

9

Simple answer: disable "show plots in scientific window" (Settings -> Tools -> Python Scientific) or downgrade the PyCharm or move your project to python3
Remember to add plt.show() in your code.

A little more complicated. You need to write own importing hooks to find that urllib.parse and urllib.request (next line in display_.py file are requested. More you can read here https://xion.org.pl/2012/05/06/hacking-python-imports/

(i'm not enough familiar with python 2 import system to write it)

Sign up to request clarification or add additional context in comments.

Comments

0

For python 2 use

from urlparse import urlparse

If you need to write code which is Python2 and Python3 compatible you can use the following import

try:
    from urllib.parse import urlparse
except ImportError:
     from urlparse import urlparse

In your PyCharm project:

  • press Ctrl+Alt+s to open the settings
  • on the left column, select Project Interpreter
  • on the top right there is a list of python binaries found on your system, pick the right one
  • eventually click the + button to install additional python modules, in your case, it is parse module is missing so install that one

5 Comments

have you tried changing python version in your pycharm?
pycharm settings Project Interpreter now is select Python 2.7, and I have try print sys.version it shows 2.7.16
what it looks to me is that your Pycharm is looking for a module which is not compatible with a particular python version.
Updated have a look
I have sovled the problem, just uncheck the checkbox "show plots in scientific window" (Settings -> Tools -> Python Scientific) and then works fine, thanks your reply
0

As mentioned by @Grzegorz Bokota, the problem is coming from the "scientific view mode" of PyCharm. This mode allows to visualise graphs and is thus calling matplotlib, and probably an incompatible version of it if you are using Python 2. This bug has been identified here and it seems that we just have to wait for the next release to get it solved.

Comments

Your Answer

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.