Error: Unknown Import 'utils' But Code Works - python

In my code I get this error telling me that it doesn't recognise my imports:
This has kind of fixed the problem, in VSCode, instead of opening a large folder which has other sub folder, I opened only 1 of the folders:
I am not sure what is causing this problem as, the code which I have written uses data and functions in the files from utils folder. This means that my app is running flawlessly with no errors even though I am getting this unresolved import warning. Is there any way to fix this? Thanks a lot and I really appreciate your help!
My app crashes when I say .utils.constants as it says that there is no parent package / module.
Here is my complete file structure in VSCode:
The happened with PyCharm:

I assume, Pycharm is not identifying the source root of the project. You can select the Local Memory directory as source root.
You can follow these steps to select the Local Memory as source root in Pycharm:
Right click on the Local Memory from Project sidebar.
Click on Mark Directory as > Sources root.
You do not need to add . before utils in import statement.
Demonstration:
I have removed similar unresolved import error from Pycharm.
Before selecting a directory as Source root, I was getting the unresolved import error in Pycharm like below:
Then I marked the utils folder as source root and the error is fixed. After the source root mark, the folder icon is shown as a blue folder icon like below:
Reference:
Pycharm documentation on Content root types

There is a problem with Pycharm.
If you want to remove the error Pycharm must scan for files to index
After indexing it will update the python interpreter then it should work !
Good luck

Related

VS Code can't resolve module after adding it to path

I'm trying to import a module controller that is located in this folder path C:\Program Files\Webots\lib\controller\python39\controller.py. My code is located in a different folder C:\Users\M----\Documents\School\ECE10\Project1\controllers\p1_controller.py therefore I'm adding the folder location to the path so I can import it as follows:
import sys
sys.path.append('C:\Program Files\Webots\lib\controller\python39')
from controller import Robot
However, VS code says that it still cannot resolve the module as if it was never imported. I'm not sure if this is a python issue of VS code issue? Any help is appreciated!
sys.path.append can not influence the IntelliSense, although you can run your code successfully the IntelliSense will still prompt the error message.
You need to modify python.autoComplete.extraPaths in the settings.json, like this:
"python.autoComplete.extraPaths": [
"C:/Program Files (x86)/Google/google_appengine",
"C:/Program Files (x86)/Google/google_appengine/lib/flask-0.12"]
You can refer to the official docs for more detail.

Pycharm mistakes folder for module

Recently I switched computers and re-downloaded my python and pycharm, I tried activating my saved projects on the new computer, but I ran into problem, pycharm doesn't recognize the parent folder of some of the files, and mistakes it for a module.
The folder is called: "Enrichment_extra_stuff", and when I try to import file in that folder, from another file in that folder, it seems like pycharm recognize it, because it shows me all of the files inside it, but when I run the code, I get the error ModuleNotFoundError: No module named 'Enrichment_extra_stuff'.
Also weirdly, when I try doing explict import and just write import fr to import the file fr, then pycharm shows an error but when I run it, it works like it should.
I tried digging a bit on pycharm, but got confused and didn't found anything, my python interpreter version is 3.8 and I program on windows if that helps.
A folder (or better a directory) is not seen as a module, unless you put __init__.py file in it. This could be empty, or it should be the module content. This is Python: a module is either a file, or a directory with __init__.py
The second part is only for Pycharm: PyCharm is created to handle large projects, and often your program is not in the base (root) directory of your project, but in one (or more) subdirectories (e.g. src). So you should explicitly tell PyCharm which are the root directories. So, go to the project structure panel (the panel with the files, usually on left side), go to your "root" directory, and set with right mouse click on your base source directory: on the pop-up menu select Mark directory as, and then select Source Root.

PyCharm editor and Python console unresolved reference

If I have a module in my project with all the inits configured properly and I try to import anything from this module, PyCharm does not identify what I am trying to import. It would work before but somehow it stopped working and I have no idea why.
Example:
./package/file.py
def function():
print('function')
./package/__init__.py
from .file import function
./call.py
from package import function
function()
The code executes fine but I can't use auto complete and the editor is informing an error "Unresolved reference 'function'" in the file ./call.py
Methods I tried that did not work:
Invalidate Caches / Restart
Use different the interpreter (but still using conda)
CTRL + Space doesn't show anything I need
Tab doesn't show anything I need
Restart PyCharm
EDIT0:
If I use import package the autocomplete works fine displaying the functions (package.function) to use while in the Python Console, but still doesn't work in the editor.
EDIT1:
I tried to mark the directory as Source Root and it did not work
EDIT2:
Tried uninstalling from snap and installing it all back again after removing all files from /home/usr/.PyCharm* and it is working fine.
You need to mark top level directory as a Source Root.
Right-Click on it, and at the bottom there is an option Mark Directory as and choose Sources Root. It will turn blue.
Then you can import:
from package import function
function()
OR
import package
package.function()
Read more at here.
Tried uninstalling from snap and installing it all back again after removing all files from /home/usr/.PyCharm* and it worked fine.

pycharm is showing error in module import resolution even if program is working fine

I am new in Python and pycharm ide.
I have created one of my own module and importing it in another python file.
Both of my python files are in same direcotary and parent directory has been made Sources Root directory.
Now my program is working fine but pycharm ide is showing error in import statement and on hover prompting no such module.
I am not sure if i am doing something wrong please let me know what is the proper way to import a module.
here is my project structure -
Main.py is importing Greeting.py and getting error.test is a root directory.
For my case, I had my problem was not resolved by invalidating the cache,
My problem was with the directories (packages) and the root directory.
Here was my structure.
MyApp:
|
|----my_first_app
|--------my_first_app_files
|--------my_first_app_directories
|----my_second_app
|--------my_second_app_files
|--------my_second_app_directories
So what I did on my editor was I right clicked app:>mark directory as:>sources root
Referrence
Link
invalidate cache and restart resolved the problem.
jetbrains.com/help/pycharm/cleaning-system-cache.html

PyCharm unresolved reference when importing class from other file

This problem has been driving me nuts. I am trying to import a class from a file in the same directory. PyCharm is giving me the "Unresolved reference" error. MyClass is defined in file.py.
I have found these questions:
Unresolved reference issue in PyCharm
Pycharm: "unresolved reference" error on the IDE when opening a working project
PyCharm shows unresolved references error for valid code
Unresolved reference when importing from sibling sub-package with
I have the following project structure:
I have marked src as the sources root...
I have set the "Add source roots to PYTHONPATH":
I have tried File -> Invalidate Caches / Restart.. (I even restarted the computer).
If I try to run it, I get the following error in the console: ImportError: cannot import name 'MyClass'
The interpreter is a virtualenv on Python 3.4 on Ubuntu x64 14.04.
If I install and import any 3rd party packages, they work fine.
If I try echo $PYTHONPATH in the terminal it returns nothing (same with env | grep PYTHONPATH. I have the appropriate virtualenv active when I try these.
Any clues?
If MyClass is defined in pack/file.py, you need to import it as:
from pack.file import MyClass
Note that using names of Python built-in types (such as file) for your own modules is a bad idea.
If you are using python version 3 try this
from .pack import myclass
This worked for me
The following steps solved my issues:
All directories required at least a blank __init__.py file
Mark all directories as source roots (per previous poster instructions)
Yes, if you are using python 3 you should add something like this:
from .pack import MyClass
It will work
I had the same issue when I tried to import a new class, however I could successfully import functions from a file in the same directory. I still dont understand why I could not import my class but thought I would share the information for other users.
#kaylebs response worked for me. However I then added the src directory to the list of source directories, first link in #lulian 's question and could remove the '.' from my file name.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files or find Text and delete __init__.py from the list of text files
I just delete the copied the code and delete the file and again create the same, that time it will work

Categories

Resources