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
Related
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)
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 am trying to import a Python module (fiasco). I cloned it from GitHub and everything appeared to be working fine. Importing it works, but when I try to type, for example iron = fiasco.Element('iron', [1e4, 1e6, 1e8]*u.K), I get the error module 'fiasco' has no attribute 'Element'. I am using Spyder's iPython console. This also fails if I start iPython from the terminal, but works if I start python3 from the terminal.
I had done this on two different computers - on one, it worked at first, but started giving me the same error after I restarted the kernel. On the other, it never worked at all.
If it helps: after importing, I tried typing fiasco. When I did this on the computer where it originally worked, the output was <module 'fiasco' from '/Users/shirman/fiasco/fiasco/__init__.py'>. Now, and on the computer it never worked on, it just says <module 'fiasco' (namespace)>. So maybe this has something to do with paths?
Addition: sys.path points to /Users/shirman, and several paths within /Users/shirman/anaconda3. The fiasco folder is in /Users/shirman.
You have inadvertently created a namespace package due to your sys.path setting. Namespace packages are directories without an __init__.py in the Python search path and allow loading of submodules or -packages from different paths (e.g. path1/foo/a.py and path2/foo/b.py can be imported as foo.a and foo.b).
The problem is that import fiasco finds /Users/shirman/fiasco first and imports it as a namespace package. If you set sys.path such that /Users/shirman/fiasco comes before /Users/shirman, the importer finds the actual package /Users/shirman/fiasco/fiasco first.
Namespace packages are a Python 3.3 feature, so either the other machine had a different sys.path setting, a really old Python 3 installation, or you were using Python 2.
I use LiClipse as my Python IDE. I imported two given skeletons in order to extend them (my first Python project). I created a PyDev project and dragged and dropped the files beneath the root of the project. The .py file declares the following import statements:
import cv2
import numpy as np
import math
Every use of cv2 or np results in an Undefined variable from input. Is this question identical to How do I fix PyDev "Undefined variable from import" errors? (with a proposed solution of just ignoring the error in the IDE) or did I forget some settings?
As those modules are built-in, it seems that you have not defined the Python interpreter for your IDE and/or the project's properties.
Set your Python installation in Project -> Properties -> Interpreter/Grammar
In case you want to use external modules, you can add them to the project by defining the PYTHONPATH in Project -> Properties -> PYTHONPATH.
I have this structure of a python project:
RF
\__init__.py
----tools
--------\__init__.py
--------drawtools.py
----examples
--------\__init__.py
--------something.py
All __init__.py are left blank. Now, in "something.py" I type:
from RF.tools.drawtools import *
And I get:
ImportError: No module named RF.tools.drawtools
What's the correct program structure? Do I have to put something in the init files?
I notice that if "something.py" is in the top directory it works. The strange thing is that PyCharm, the IDE I'm using, seems to recognize the import and give me code completion.
I heard something about setting PYTHONPATH but as this project must be shared in a team I'd prefer to keep things as simple as possible (you copy the project from one to one and run it without any annoying importError).
I think that it would work with a relative import, such as
from .. import drawtools