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.
SHORTER VERSION:
If you have squiggly line below module you import...
...move on name of module and press Alt+Enter and select 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.
The window that opens has some specific regions:
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.
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.
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).
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.
A package should be installed and listed now in installed packages.
PyCharm creates a virtual environment with its own Python Interpreter for your project, you need to install the module for the interpreter you are using. To do this go to Settings -> Project: yourProjectName -> Python Interpreter, click the plus icon and select the module you would like to install.
Alternatively you could force PyCharm to use your other Interpreter by selecting it from the dropdown at the top of the Python Interpreter settings page I mentioned before.
There are many reasons for this.
The reason and fix I'm going to put here is extremely rare, just decided to put it hoping at least a single person can get help from this answer.
The issue
PyCharm treats __init__.py as a non python file.
This happens when you forgot to use .py extension in your code's __init__.py files, and then you add the extension later. Then pycharm starts treating all __init__.py files (even external library files) as non python. How strange?
Detect if the issue is this
Scroll click on the library name (not the module name).
Or right click and goto Declaration or Usages
You will see the library's __init__.py as plain text.
If you see the code as plain text, that's the issue!
The Solution
Find the file location in pycharm navigation. If you are using virtual environment, the file fill be in venv/lib/site-packages/{library_name}.
Right click on __init__.py, click override file type. Select python.
You'll see the error is gone!
Related
I'm having ImportError: cannot import name 'Literal' from 'typing' error message when I try to use pip install. Anyone can help?
The problem is with the configuration of your environmental variables. The first thing, I'd suggest you do is uninstall all the unused versions of python. To my mind, the latest of python 3.x.x series is the best.
Steps:
The easiest way of accessing it is just to type environment variable in Windows 10 search bar. (Assuming you are using Windows 10)
Then follow these:
At the bottom half part of the subsequent window search for path:
If you double click that, look for python. Make sure you have such paths added there and you need to have a compatible version of python. Usually, your python bin directory is located in Program Files.
I have a problem with importing module pyinputplus to my project.
I type 'pip install pyinputplus' in cmd (iam using windows), all was fine, cmd showed that installation was succes.
But when i try: import pyinputplus in my project in PyCharm i have an error: ModuleNotFoundError: No module named 'pyinputplus'
Some weeks ago i installed packed named selenium, and everything was fine. All works with no problem. I do the same with pyinputplus and its wrong...
Moreover, i tried to use pyinputplus in cmd and all was also fine. Where is a problem?
Pycharm creates virtual environments for each project, so installing it system-wide (as you just did) doesn't make it available to your pycharm project.
Easier Solution
The easy solution is to go to preferences, click "python interpreter" (you may need to expand "project"), then click the plus icon at the bottom of the package list, and search for pyinputplus and click install. It is now installed, but won't work for any other project.
Long-term Solution
Keeping packages seperate has it's advantages, like allowing you to have many different versions of packages, and avoid conflicts, but you may prefer for everything to use the same interpreter. If so, go to the same "python interpreter" page in preferences, and change the interpreter in the dropdown to the other available one. If there are more than 2, you may need to use trial and error.
Have a look at the documentation here: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html#interpreter-settings
I have downloaded Pandas library with pip install pandas through the command prompt, when I try to import pandas as pd PyCharm returns an error : ModuleNotFoundError: No module named 'pandas'
I have tried to uninstall and install again many times but nothing seems to work. Does anybody know a solution to this?
You can try downloading the library from PyCharm settings:
File -> Settings
then, Project: -> Python Interpreter
Click a + sign to the right,
Search for the pandas library,
and finally, press 'Install Package'
I think you have to choose the right python interpreter. Check my screenshot
You likely have multiple copies of Python installed on your system. PyCharm can be configured to use any version of Python on your system, including any virtual environments you've defined. The solution is to match up the version of Python you've installed Pandas into with the version of Python that PyCharm is using to run your code.
There are two places where you specify a Python version. First of all, your Project has a version associated with it. Check the "Python Interpreter" section of the "Project" section of your Preferences for that. That version is used for syntax highlighting, code completion, etc.
By default, the abovementioned Python version will also be used to run your code. But you can change the version of Python that your code is run with by creating or modifying a Run Configuration. To do this, check the menu next to the Run and Debug toolbar buttons near the top-left of your PyCharm window.
When you do get into the Python Interpreter section of the Preferences, you'll find that you can see all of the modules installed for each Python version that PyCharm knows about. You can use this to check to see if Pandas is installed for a particular Python version.
I would suggest you get comfortable with all that I've said above. It will save you many headaches in the future.
I'm getting ModuleNotFoundError: No module named 'macrobond_api_constants'although the package seems to be installed correctly. I checked using conda list command.
https://pypi.org/project/macrobond-api-constants/
The instruction says this should be imported using import macrobond_api_constants
I tried to import using both Spyder and PyCharm. Any idea how to fix this?
In pycharm import libraries exist in different way. You can try this
1. Open PyCharm.
2. go to File -> settings -> Project:test(test means your project name) -> select project interpretor -> click add button
3. after clicking add button and search the library which you want to install then install it.
finally run the program.
Thank you everyone that wanted to help me with this issue. I contacted lib authors (Macrobond) saying I cannot access library and turns out there was something that they messed. An update has been just released couple days ago. Library is now accessible and works pretty well.
When I write import MySQLdb in Eclipse using the PyDev plugin, I get an unresolved import. However, the program runs without error. I can add an annotation to get the error to go away, but what is the right way to handle this?
How can I help Eclipse know that MySQLdb is there?
It sounds like MySQLdb is somewhere on your sys.path, but not on your Eclipse project's PYTHONPATH; in other words, Eclipse thinks you're going to get an import error at runtime because you haven't fully configured it. Google seems to say that you can alter this setting in Window->Preferences->Preferences->PyDev->Python Interpreter to include the path to your MySQLdb module.
For some help figuring out where MySQLdb might be living on your system:
Open an interactive interpreter,
import MySQLdb
If that succeeds, you can get a hint from: print MySQLdb.__file__; it may be the __init__ file in the package that you need to point the path at.
cdleary above provided the reason two years ago, but this may be easier. Basically, one reinstalls the interpreter.
Select Window - > Preferences -> PyDev -> Interpreter - Python
Select the python interpreter in the upper pane
Click on Remove
Click on Auto Config
Agree to everything.
This works on Fedora 17 using the Eclipse 4.2.0 that came with the package management.
Fixed this by doing two things:
1) Added MySQLdb egg to the PYTHONPATH under Window->Preferences->Preferences->PyDev->Python Interpreter.
C:\Python26\Lib\site-packages\MySQL_python-1.2.3c1-py2.6-win32.egg
2) Close and re-open the .py file that had the red x.
Adding the egg works, but the error remains. The solution for that error can be found by adding
##UnresolvedImport
To the import statement, as in:
import web ##UnresolvedImport
Source: http://klaith.wordpress.com/2009/06/12/pydev-unresolved-import-errors/
I once had a similar problem on Windows (never encountered this on Linux though) and I discovered that I had to include the .egg directory of my library to my PYTHONPATH.
For example my PYTHONPATH (Pydev/Interpreter - Python/Libraries) included:
C:\Python26\Lib\site-packages
and I had to add:
C:\Python26\Lib\site-packages\jinja2-2.2.1-py2.6.egg
to use jinja.
This surely works I just tried it with Pmw package. Unzip package in site-packages. Then remove python interpreter from eclipse and then add it again. Your import errors shall go away. also you may want add module to forced builtins. See How do I fix PyDev "Undefined variable from import" errors? and http://pydev.org/manual_101_interpreter.html
I had a similar issue and the following is what I did to solve my issue. I have a Windows 8 Machine, Python 2.7 installed and running my stuff through eclipse.
Some Background:
When I did an easy install it tries to install MySQL-python 1.2.5 which failed with an error: Unable to find vcvarsall.bat. I did an easy_install of pip and tried the pip install which also failed with a similar error. They both reference vcvarsall.bat which is something to do with visual studio, since I don't have visual studio on my machine, it left me looking for a different solution, which I share below.
The Solution:
Reinstall python 2.7.8 from 2.7.8 from https://www.python.org/download this will add any missing registry settings, which is required by the next install.
Install 1.2.4 from http://pypi.python.org/pypi/MySQL-python/1.2.4
After I did both of those installs, I reopened eclipse and got a prompt to update the paths of eclipse which I accepted, after that I was able to query my MySQL db.
import MySQLdb
If this code show error like this:
Unresolved import: MySQLdb
you should add D:\Python27\Lib\site-packages\MySQLdb to your sys.path.
D:\Python27\Lib\site-packages\MySQLdb is this location where you install MySQLdb in your computer disk. After this step, the error will disappear.