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

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.

Related

How to set the correct environment variable for python scripts?

I have been trying to add several paths to environment variable in windows-10 but still repeatedly getting Module not found error?
Mycurrent path to envirionment variables
when I try to run my training script I get Module not found error.
error while executing
Please help me to add correct path to envirionment so that I would not get "module not found error" ever again.
you can give the path to python script like this
<python installation path>\Scripts\
you can check the python installation path in windows by entering this command in cmd.
where python
It looks like a pretty complex directory structure. I wonder whether the fact you have a subdirectory with the same name (regression_model) as its parent is causing problems?
In general what you need to make the following happen:
the module you want to import should be a directory with an __init__.py file inside it
the parent directory of the module directory should be in the pythonpath
You could try renaming one of the directories and simplifying what's in the environment variable to match the above.
If that doesn't work
import sys
print(sys.path)
to check what the script thinks is in the pythonpath.
(you can even try using sys.path.append within the script to put the right thing into the path if all else fails! Might help with debugging.)

Error: Unknown Import 'utils' But Code Works

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

How do I set the search path for a Python import?

I'm using Windows 10 with VS2015 and Python 3.4. I have created a module called this.py. Now I want to import this.py. That works OK if this.py is in the current folder. But I need this.py to be in a different folder which is used by all of my scripts.
I have tried a few approaches but none would find this.py. One approach was to set my Windows CMD PATH.
Can someone tell me how to set the search path for imports?
You should do something like this:
import sys
sys.path.append("path-here")
Then that directory will be searched when you import something (other than standard directories)
Edit: the path of the directory containing the imported file, not of the file

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

Finding import path in pycharm

I am using pycharm version 3.4 on ubuntu and have a project with the following structure:
~/project/src/python/utils/sub1/sub2/sub3/my_code.py
the utils folder also contains an __init__.py file which offers a number of utility functions. I want to include some of them, but it wouldn't find it:
from project.src.python.utils import read_utf8
I followed this post, which seemed to discuss the same problem:
PyCharm can't find the right paths if I open a directory that is not the Django root
But changing ~/project to a "source" folder didn't help. This isn't a typo on my side, as it should at least find "project" when trying to import something from it, but
import project
also gives my an "unresolved reference" error.
edit: I should add that I cannot change the code, as it is a shared project. I need that exact import line to work on my machine. My counterpart uses eclipse, were it seems to be easier to add a path to additional code.
It looks like the source isn't in your PYTHONPATH which is why you're unable to import it.
python only looks in certain places for py/pyc/pyo etc. files and modules to import (the places are the paths listed in sys.path). You can add custom places for it to look with the PYTHONPATH environment variable, or in PyCharm under preferenes > project interpreter > configure interpreters, and then adding any paths in the paths section.
I can't tell which folders are actually modules but from the names of the folders I would guess only utils. If that's the case add the path /home/ceca/project/src/python to the list of paths in PyCharm and in your code from utils import read_utf8

Categories

Resources