Module calling in Pydev plugin in Eclipse - python

I have Pydev installed in Eclipse and everything works fine. I can import other modules installed through "pip install xxx" without any warnings or errors.
However, I got such a warning when I created two files to test:
test/HelloWorld.py
def hello():
print('Hello World')
test/HelloWorldTest.py
from HelloWorld import hello
def test():
hello()
I can run this "HelloWorldTest" without problem, which correctly prints "Hello World". However, in my PyDev editor, it always warns that:
hello Found at: test.HelloWorldTest
from HelloWorld import hello
―――――――――――――――――――――――――――――――――――――――――
Unresolved import: hello
Why does it show that 'hello' is unresolved import, which is highlighted in red? However, I can still run it with correct result.
Very strange!
Edited:

Maybe your IDE is pointing to a different Python interpreter.
I do not use Eclipse but it happens with PyCharm as well.
In PyCharm I can select a different Python interpreter for the code editor and a different one for running. Normally I would not select different interpreter for the same project and confuse myself but it is possible for edge cases.
I think similar thing is happening with your Eclipse IDE. Please double check your settings and make sure the code editor is pointing to the right Python interpreter.
If you are working on multiple Python projects, it is highly recommended to use some sort of Python Virtual Environment to avoid dependency conflicts. PyEnv (https://github.com/yyuu/pyenv) is a good one if you are on Linux or Mac.
Im pretty sure the path for the Python interpreter is not set properly on your IDE.
Answer updated
Why does your folder icons look so different? Are they inside a zip folder or something?
It works perfectly fine on my machine. I do not see any errors.
Please check my settings.
Window > Preferences > type python interpreter in the search box. Try removing the interpreter and manually add again.
Right click on the project folder > Properties > type interpreter in the search box.
Hopefully this solves your issue.
Python Virtual Environment
As you are working on many different projects, it is highly recommended to use a Python Virtual Environment if you haven't already. PyEnv (https://github.com/yyuu/pyenv) is a good one if you are on Linux or Mac. Or you can use the default VirtualEnv with VirtualEnvWrapper if you are on Windows.
Quick note on Coding Style
It has nothing to do with your question but I think its worth mentioning.
I highly recommend you to enable PEP8 feature on your IDE because it helps you to make your project and code much more consistent.
It is recommended to have module names in lowercase.
ref = https://www.python.org/dev/peps/pep-0008/#package-and-module-names

Related

how to change module path for python in visual studio code

I'm sorry if this is trivial, but I'm just starting python and I don't know a lot of terminology so bear with me.
background information:
I just recently changed my IDE from anaconda (Spyder) to Visual Studio Code and I realized that a lot of modules that I installed from pip and used to able to import on Spyder is now unavailable on Visual Studio Code. The only work around that I have been able to come across is to import sys and append the appropriate path to every single one of my project. This is obviously very annoying and is quite a bit of a hassle every time I try to start a new project. Since Spyder works fine for me, I'm convinced that the problems lies in the Visual Studio Code settings instead. But every articles and advices I've came across couldn't give me a straight answer without throwing terminology that I'm unfamiliar with left and right.
Request:
Can anyone help me to permanently append a new python module path to Visual Studio Code so that I don't have to import sys every time?
Thanks in advance!
You can use python interpreter path setting to point to a python installation/ venv
Set the path in .vscode/settings.json
{
"python.defaultInterpreterPath": "path to python.exe"
}
You can follow this for more info.
Python extension has a function called Python: Select Interpreter, which you can get from Command Palette.
If you didn't create virtual environment when using Anaconda, modules should be installed globally, for example python3.9.5.
If you installed modules in virtual environment, Python extension should also be able to detect and show them in the list by click the command Python: Select interpreter. If not, you can manually Enter interpreter path:
Choose the environment where locates your needed modules as the interpreter then you may import them successfully without extra import sys.
More information please refer to Using Python Environments in VS Code.

VSCode not selecting the correct python interpreter

I have a script that I need to run using a very specific python interpreter in my hard drive.
I am using vscode as the IDE and trying to select the python interpreter using the command palette.
However, since the concerned interpreter is not present, I manually search it.
However, even after that, it doesn't work as typing python in the terminal then opens the windows store for installing a python IDE. I also tried adding the interpreter's path to the environment variables but the problem still persists.
How do I solve this issue.
Thank You for the help.
The Python version you selected on VS Code only effects the editor. Right click to any file on the sidebar and select Open in Integrated Terminal. If your selected Python version is created with virtualenv it will activated and your python command works.
If not worked and you want a global Python version, you need to add Python path to your system variables to work python command. Check this:
https://geek-university.com/python/add-python-to-the-windows-path/
Btw I don't think your python.exe in FreeCad/bin directory is proper Python version
At the bottom left you can select the Python interpreter.
Here it shows you on -how to do that: Python Interpreter
Reason:
Your system environment path contains this: %USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
Solution:
Open Window Search(Win) to find Manage app execution aliases. To turn off App Installer of python3 like this:
You can refer to this answer for more information.

Import "flask" could not be resolved from source Pylance (reportMissingModuleSource)

When I am writing
from flask import Flask
One Yellow line is coming up under flask and stating Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) . Also, I am able to do the work with this package also successfully. But the thing is, I am not able to use autosuggesstions for Classes and methods very well.
Further:
I have checked that flask is installed successfully.
Also I visited this thread https://github.com/microsoft/pylance-release/issues/236
And I set up my settings.json in vscode as follows:
"python.analysis.extraPaths": [
"/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]
But Still, unable to resolve the error.
When I did not install the module "flask" in the Python environment currently used in VSCode:
Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:
Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)
If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.
(F1, Developer: Reload Window)
Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.
To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.
I had a similar issue while trying to import flask on vscode.
I fixed it by using anaconda. Simply you install the flask module in your created environment example screenshot.
How to create a virtual env in anaconda:
1. On the left sidebar, click on environments.
2. Click create (at the bottom).
3. At the pop-up window, give your vir.env a name
and select the language version.
4. Once created, you can start installing
different modules in your environment.
I hope that helps!
For Linux Mint and for those who have installed flask, but VSCode doesn't find it:
check the Flask path: pip show flask (should be smth like
Location: /home/<username>/.local/lib/python3.8/site-packages
in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path.
That's because you have not chosen your path correctly,
type:
pipenv --venv
then it will show you where your virtual env is installed. Check where the packages are installed in your env, and then type what comes to you from the shell\scripts or whatever\python, and the packages will work.
I solved my problem by using a "Global" version of Python. Maybe Pylance had not updated to work with the version of Python I was using.
interpreter language screenshot
I was facing same issue. I tried all solutions from stack-overflow but none worked. But after lot of searching and time waste I found my silly mistake. I had created folder named 'flask' and stored my project there. I'm beginner and going through lot of such silly mistakes. Hope it would help, if somebody commits same mistake.
I think this is a Vscode problem ,just restart the Vscode .
By the following steps, I solved this issue:
1)On the project directory create the .flaskenv file
In the .flaskenv file write the following two lines:
FLASK_ENV=development
FLASK_APP=main.py
Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.
A few answers (Jill's, Marius's, and Roy's) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly. I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.
It is counterintuitive to choose both Python interpreter and notebook's Python kernel to make things work. It is even more counterintuitive considering the fact that Python interpreter's button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open. For instance, in this screenshot, we see the little line under Scikit-learn's import, indicating a problem with the import (even though the import is successful). However, the correct Python kernel, with Scikit-learn installed, is already chosen. Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn. In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab). I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code. They could ignore the other answers because they could think it is not the case for them.

Two python3 interpreters on win10 cause misunderstanding

I used win10. When I installed Visual Studio2017, I configure the Python3 environment. And then after half year I installed Anaconda(Python3) in another directory. Now I have two interpreters in different directories.
Now, no matter in what IDE I code the codes, after I save it and double click it in the directory, the Python File is run by the interpreter configured by VS2017.
Why do I know that? I use sys.path to get to know it. But when I use VS2017 to run the code, it shows no mistake. The realistic example is that I pip install requests in cmd, then I import it in a Python File. Only when I double click it, the Traceback says I don't have this module. In other cases it works well.
So, how to change the default python interpreter of the cmd.exe?
Just change the interpreter order of the python in the PATH is enough.
If you want to use python further more, I suggest you to use virtual environment tools like pipenv to control your python interpreters and modules.

Python OpenGL in Eclipse/Aptana

I am trying to import the OpenGL.GL module.
Given the py file with that line, I can perform "python file.py" just fine, but I cannot run that same file when used in Aptana or Eclipse. Both IDEs have PyDev installed.
I do have PyOpenGL installed.
I wish to point out that I can still import other modules (PIL, numpy), which were installed the same way as the PyOpenGL. I am confident that there is only 1 python running on my MacOS.
I had the same problem after installing a different version of PyOpen and my Eclipse PyDev path is messed up. What I did was remove the interpreter link and re-added the old one which made PyDev to re-scan my libs. This seems to fix the problem. Don't forget for all your projects, you need to go to the property (Right click project->properties) and re-select the interpreter.
Maybe you need to reconfigure your interpreter.
If you installed PyOpenGL as an egg after pydev was set up your PYTHONPATH might be out of date.
Check out Preferences->PyDev->Interpreter - Python
Are you sure you don't have multiple versions of python? Seems to me like the interpreter that aptana uses is not the same as the one used from command line. You can look in:
Run -> Run configurations -> Python run -- then you have Interpreter tab
There you can click : See resulting command line. Than will get you the python that is used as well as the python path

Categories