Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

python - PyCharm: ImportError no module named X?

X in this example represents any module or package you install.

The problem:

I have a problem with a package called 'X'. In PyCharm I get an error ImportError: No module named 'X'. My code runs from the terminal without any problem. Any help ?

or

I installed X on python3.4 with pip. In terminal, when I import X, everything is fine, but when I import it in PyCharm, it says: ImportError: No module named 'X'". Any help ?


This is basically everyday question, just with different module or a package instead name instead of X.

And it's not problem in a package or module you install, it's in a PyCharm Project Interpreter you are currently using for your project.

The following answer is usually solution to this specific problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SHORTER VERSION:

If you have squiggly line below module you import...

No Module found

...move on name of module and press Alt+Enter and select Install package X.

Install package X

This should (probably !!!) install module you thought you installed, but got ImportError.


LONGER:

If you want to use module X:

Go to File -> Settings -> Project:NameOfProject -> Project Interpreter.

Project interpreter image

The window that opens has some specific regions:

  1. This is a project interpreter PyCharm is currently using. You can change you python environments here also. If you want to add virtual environment you created manually, continue reading.
  2. A little "cog" or a "gear" is used to Add, Edit or Remove environments. If you want to add virtual environment you created manually, select Add... when gear icon is clicked, and make sure to set proper path to python.exeof your virtual environment.
  3. IMPORTANT: A list of all installed packages, represented by: Package name and Version. If you tried to use package, but got ImportError no module named 'X', make sure to check if package is listed here!!!. If not, it's not installed in the current python environment and it should be installed (continue reading).
  4. A little + represents Install. It can be used to install packages. Simply click on + sign, search for a package and click Install Package at the bottom after you found it. You can also specify the version you want of a package. In example below, we searched for flask package.

    Package installation A package should be installed and listed now in installed packages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...