I am coding in PyCharm and to set up the environment I use poetry, python version 3.10
Everything was working ok, but starting from one moment of time I got problem with module imports.
But the problem does not relate to the import of the whole module but to the import of class or function from module.
You can see as an example that BaseSettings from pydantic is underlined with red wavy line and Pycharm says that BaseSettings is unresolved reference.
I tried
to invalidate cache in Pycharm
install, uninstall the module (poetry add/remove)
change the environment and return back
marked folder as a source root (assume it is irrelevant but anyway)
but unfortunately nothing helped
aiohttp.client.py
aiohttp.init.py
I found the out what was the reason for this strange behaviour. It was the problem of PyCharm IDE. PyCharm stopped recognising init.py as python files that produced import errors. Actually I do not know why this happened since I did not changed any settings or updated PyCharm.
The solution was to manually add pattern "init.py" to python files mask in Settings -> Editor ->File types -> Python (please see screenshot in the attachment)
Related
The Summary
I have a python import that works when run from the VS Code terminal, but that VS Code's editor is giving warnings about. Also, "Go to Definition" doesn't work.
The Problem
I have created a docker container from the image tensorflow/tensorflow:1.15.2-py3, then attach to it using VS Code's "Remote- Containers" extension. Then I've created the following file in the container.
main.py:
import tensorflow.compat.v1 as tf
print(tf.__version__)
This runs fine in the VS Code terminal, but the Editor and the Problems pane both give me an unresolved import 'tensorflow.compat' warning. Also "Go to Definition" doesn't work on tf.__version__.
I'm using several extensions but I believe the relevant ones are the Microsoft Python extension (installed in the container), as well as the Remote - Containers extension, and now the Pylance extension (installed in the container).
The Things I've Tried
I've tried this with the default pylint, and then also after installing pylance with similar results. I've also seen some docs about similar issues, but they were related to setting the correct source folder location for modules that were part of a project. In contrast, my code within my project seems to work fine with imports/go-to-definition. It's external libraries that don't seem to work.
Also, for the sake of this minimal example, I've attached to the container as root, so I am guessing it's not an issue of elevated permissions.
I've also tried disabling all the extensions except the following, but got the same results:
Remote - Containers (local)
Remote - WSL (local)
Python (on container)
Jupyter (on container, required by Python for some reason)
All the extensions above are on the latest versions.
I've also fiddled around with setting python.autocomplete.extraPaths, but I'm not sure what the right path is. It also seems like the wrong thing to have to add libraries to the path that are installed in the global python installation, especially since I'm not using a virtual environment (it being in a docker container and all).
The Question
How do I fix VS Code so that it recognizes this import and I can use "Go to Definition" to explore these tensorflow functions/classes/etc?
tldr;
TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.
To Fix:
pylint: The pylint warnings are safely ignored.
Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as x then running help(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.
Details
Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:
import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()
The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:
import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()
This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.
However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.
Confirmation/Tickets
pylint: I've found a ticket about pylint having issues with a couple tensorflow imports that looks related.
Intellisense: I've opened a ticket with pylance.
So for me I was trying to
import pandas as pd
but I got the error
"pd" is not accessedPylance (module) pd
SO what I did was reload the extension Python IntelliSense(Pylance) and that solved my issue.
I had the same problem but with all kinds of packages.
My solution was to go to the VSCode settings and search for "python.analysis.extraPaths", and add the path to your site-packages.
In my case, I added C:\Code\Python39\Lib\site-packages, and now it's working fine.
What, usually, solves the pylance issues for me is pointing my Python interpreter to the virtualenv one.
Open the command palette Ctrl + Shift + P
Type: Python: Select Interpreter
It will show a list of all the python Interpreters it actually detects:
Select Enter interpreter path
Type in the path to your local venv/bin folder or click find to navigate using the file explorer.
Your path should look something like:
venv/bin/python3.9
i changed "import tensorflow as tf" to "from tensorflow import compat as tf"
it ll even work for tf.gfile.Gfile()
I had a few imports I was using for my project. Some of these included packages I downloaded using pip like
import MySQLdb
While others were modules from within my project like
sys.path.append(os.path.join(os.path.dirname(__file__), "../")) .
from util.myFile import get_mysql_connection, execute_query,
These were all working and I was able to run my application on PyCharm. I closed PyCharm and opened it back up to find most of these imports "could not be found". Any help regarding this will be much appreciated. Thanks
Update:
I'm able to run the file from terminal (iTerm) without errors. However, the file still shows red error marks by the imports. Seems like PyCharm isn't recognizing some sort of path
Check the traceback for what version of python is being called.
There could be a mismatch between the version you've imported modules with and the version that runs by default.
Double check what is being used as your project interpreter. This can change the available packages to your current project.
Found in PyCharm by going to Settings -> Project:<'Your-Project'> -> Project Interpreter
Also, did you install MySQLdb in this way? MySQLdb - ModuleNotFoundError
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!
I have a very strange pycharm behavior. I have a project and a folder in it named core (see picture for details). In it i have two python files: AgentMeasurement.py and CollectorBase.py. Now, i want to import type AgentMeasurement from AgentMeasurement.py in CollectorBase, so i write the following: from AgentMeasurement import AgentMeasurement. It works fine (when i run script), but Pycharm marks this as unresolved reference.
I tried to mark core as Source Root, but then i can't reference it from other folders as package (i.e. from core.AgentMeasurement import AgentMeasurement) and can write from AgentMeasurement import AgentMeasurement which makes my code unreadable. How can i make Pycharm work correctly in such a case?
Since core is where your PyCharm project is stored, it references the modules from this folder.
By adding core.XXX in front of the module, PyCharm drops its warning.
from core.AgentMeasurement import AgentMeasurement
I am trying to work with PIL in my project but the pydev can't seem to find it in my project. First of all I can see it when I enter the python shell, I can import it and I see it in the python sys.path. Second, I Added it to the PYTHONPATH in eclipse.
I restarted eclipse, but still, when I try to do "from PIL import Image" I am getting: "unresolved import".
Can any one please help me here, all other packages I used until now worked great the same way.... and i really need to use PIL
Try to go to Window -> Preferences -> Pydev-> Interpreter -> Python Interpreter -> Forced Builtins tab.
Then add a PIL entry and apply.
I've had the same unresolved import error when tried to import from this particular package (other packages worked fine), and found this information which finally helped me.
Had the same problem here.
Got it resolved by adding /usr/share/pyshared to the Libraries tab in window->preferences->pydev->Interpreter - Python.
There were a lot of /usr/lib/python* paths with the compiled libraries (the C stuff with python bindings) where included already, but not /usr/share... parts with the source.