microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.33k stars 1.19k forks source link

python unresolved import #11934

Closed fdq09eca closed 4 years ago

fdq09eca commented 4 years ago

image

image

everything is installed and the script runs properly but the problems persist..

ericsnowcurrently commented 4 years ago

@fdq09eca, thanks for letting us know about this. It looks like the Python you have selected isn't the same one into which you installed those modules. What Python executable have you selected? (In the bottom left corner click on the "Python 3.X.X".) Also, please provide the content of the "Python" output panel.

Once you have the Python executable path, try running <python> -c 'import sqlalchemy'.

fdq09eca commented 4 years ago

@fdq09eca, thanks for letting us know about this. It looks like the Python you have selected isn't the same one into which you installed those modules. What Python executable have you selected? (In the bottom left corner click on the "Python 3.X.X".) Also, please provide the content of the "Python" output panel.

Once you have the Python executable path, try running <python> -c 'import sqlalchemy'.

Thank you for your reply. I have no idea if I am doing it right, but the issue is solved after appending the following line to .zshrc

export PATH="$PATH:/Users/macone/Library/Python/3.7/bin"

However, which python3 produces

/usr/local/bin/python3

if there is any worth, here is the python interpreter options image

and i am using Python3.7.4 image

if possible, I would like the pip3 install goes directly to the respective path of /usr/local/bin/python3 instead of the extra line added to .zshrc

ericsnowcurrently commented 4 years ago

@fdq09eca, thanks, that helps. Looks like you have /usr/local/bin/python3 configured (or it was auto-configured). I'm guessing that pip was installed via a different version (try looking at cat `which pip3`). Per the output you provided, pip is installing into /usr/local/lib/python3.7/site-packages. I expect that /usr/local/bin/python3 is using a different site-packages (try looking at /usr/local/bin/python3 -c 'import sys; print(sys.path)).

fdq09eca commented 4 years ago

@fdq09eca, thanks, that helps. Looks like you have /usr/local/bin/python3 configured (or it was auto-configured). I'm guessing that pip was installed via a different version (try looking at cat `which pip3`). Per the output you provided, pip is installing into /usr/local/lib/python3.7/site-packages. I expect that /usr/local/bin/python3 is using a different site-packages (try looking at /usr/local/bin/python3 -c 'import sys; print(sys.path)).

Thank you. I looked at /usr/local/bin/python3 -c 'import sys; print(sys.path) but it seems nothing there, see below. image

the cat `which pip3` result is the following: image

ericsnowcurrently commented 4 years ago

Sorry, that should have been /usr/local/bin/python3 -c 'import sys; print(sys.path)'. (Note the trailing single quote.)

As to which pip3, it appears to be using /Library/Frameworks/Python.framework/Versions/3.7. Is that the same as '/usr/local/bin/python3`?

fdq09eca commented 4 years ago

@ericsnowcurrently Sorry for the late reply, I missed this notification.

for /usr/local/bin/python3 -c 'import sys; print(sys.path)', the result is the following

~/Documents » /usr/local/bin/python3 -c 'import sys; print(sys.path)'                                                                                                macone@Macs-MacBook-Pro
['', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/macone/Library/Python/3.7/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

for cat `which pip3`, I am sorry that I do not understand what you mean..

should I change the /Library/Frameworks/Python.framework/Versions/3.7 to '/usr/local/bin/python3'? How can I do it?

if any worth, here is the result for which pip3

~/Documents » which pip3                                                                                                                                             macone@Macs-MacBook-Pro
/usr/local/bin/pip3

Thank you!

ericsnowcurrently commented 4 years ago

First of all, regardless of the issue of where pip3 is installing packages, please make sure to invoke pip as a module rather than a command, e.g. /usr/local/bin/python3 -m pip install sqlalchemy. (I should have asked you to try this earlier.) This ensures that pip installs the packages relative to that Python. Using the pip3 command directly is less of an issue when you only have one Python installed, but I recommend always using python3 -m pip ... anyway. If that doesn't fix the problem then please let us know.

As to the original reported problem, what we see here is that /usr/local/bin/python3 is using /Users/macone/Library/Python/3.7/ for "sys.prefix". I'm guessing that /usr/local/bin/python3 is actually a symlink. (You should be able to run something like type /usr/local/bin/python3 to see.) Consequently, for the installed packages to be available to that Python, pip3 must install packages into /Users/macone/Library/Python/3.7/lib/python/site-packages or /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages. It still isn't clear where pip3 is installing them. It should have said when you installed sqlalchemy, etc. You can also ask pip afterward to show you where it installed one or more packages: pip3 show sqlalchemy flask. I expect it will show a directory that is not on the sys.path that you reported. I also expect that /usr/local/bin/python3 -m pip show sqlalchemy will provide no output and that sqlalchemy will not show up in the output of /usr/local/bin/python3 -m pip list.

fdq09eca commented 4 years ago

It seems all of them are installed to /Users/macone/Library/Python/3.7/lib/python/site-packages

~/Documents » pip3 show sqlalchemy flask                                                                                                                             macone@Macs-MacBook-Pro
Name: SQLAlchemy
Version: 1.3.17
Summary: Database Abstraction Library
Home-page: http://www.sqlalchemy.org
Author: Mike Bayer
Author-email: mike_mp@zzzcomputing.com
License: MIT
Location: /Users/macone/Library/Python/3.7/lib/python/site-packages
Requires: 
Required-by: 
---
Name: Flask
Version: 1.1.2
Summary: A simple framework for building complex web applications.
Home-page: https://palletsprojects.com/p/flask/
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: /Users/macone/Library/Python/3.7/lib/python/site-packages
Requires: click, itsdangerous, Werkzeug, Jinja2
Required-by: Flask-Session
~/Documents » /usr/local/bin/python3 -m pip show sqlalchemy                                                                                                          macone@Macs-MacBook-Pro
Name: SQLAlchemy
Version: 1.3.17
Summary: Database Abstraction Library
Home-page: http://www.sqlalchemy.org
Author: Mike Bayer
Author-email: mike_mp@zzzcomputing.com
License: MIT
Location: /Users/macone/Library/Python/3.7/lib/python/site-packages
Requires: 
Required-by: 
ericsnowcurrently commented 4 years ago

Honestly, I was expecting all those to be installed elsewhere. So something else is going on here. I did notice that the screenshot in your original post indicates a different installation location, which would align with the problem. On top of that, it looks like sqlalchemy wasn't part of that original problem. Let's check the following and see where things are at:

Also, the original entries in the "Problems" panel were reported by the language server. Please verify that is the case. In your workspace settings.json (.vscode/settings.json), what is "python.jediEnabled" set to? What about "python.languageServer"? Please also provide the content of the Python output panel.

Kyitrai commented 4 years ago

I am having a similar issue, though my environment setup is a bit different. I'm using poetry for managing the environment and dependencies.

The behavior is that: any new packages added to the project (poetry add <package>) after starting up VSCode are not found (unresolved_import). Running the code from the terminal inside VS Code or a detached terminal with the venv activated does not encounter any import errors.

image

Poetry is set to instantiate the virtualenv in the project folder, so .vscode/settings.json has the setting "python.pythonPath": ".venv/bin/python". The environment selection according to the status bar is also correct.

image

I am using the Microsoft language server:

{
    "python.jediEnabled": false,
    "python.languageServer": "Microsoft",
}
ericsnowcurrently commented 4 years ago

@Kyitrai, please open a separate issue and refer back to this one. I suspect your situation is a slightly different problem and we don't want that to get lost in this issue. Thanks!

fdq09eca commented 4 years ago

Hi, @ericsnowcurrently, here is the required information, sorry for being late for long. I have done quite a few adjustments.. but none of them works, please treat it as a new issue. I reinstalled pip3 and every python package.. and now all of them are under /Users/macone/Library/Python/3.7/lib, again I would like it to be in /usr/local/bin/python3

/usr/local/bin/python3 -m pip show flask_session

Name: Flask-Session
Version: 0.3.2
Summary: Adds server-side session support to your Flask application
Home-page: https://github.com/fengsp/flask-session
Author: Shipeng Feng
Author-email: fsp261@gmail.com
License: BSD
Location: /Users/macone/Library/Python/3.7/lib/python/site-packages
Requires: cachelib, Flask

/usr/local/bin/python3 -m pip install -r requirements.txt

~/Documents/cs50w-pset2(master) » /usr/local/bin/python3 -m pip install -r requirements.txt                                                                                macone@Macs-MacBook-Pro
Requirement already satisfied: Flask in /Users/macone/Library/Python/3.7/lib/python/site-packages (from -r requirements.txt (line 1)) (1.1.2)
Requirement already satisfied: Flask-SocketIO in /Users/macone/Library/Python/3.7/lib/python/site-packages (from -r requirements.txt (line 2)) (4.3.0)
Requirement already satisfied: Werkzeug>=0.15 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Flask->-r requirements.txt (line 1)) (1.0.1)
Requirement already satisfied: click>=5.1 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Flask->-r requirements.txt (line 1)) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Flask->-r requirements.txt (line 1)) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Flask->-r requirements.txt (line 1)) (2.11.2)
Requirement already satisfied: python-socketio>=4.3.0 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Flask-SocketIO->-r requirements.txt (line 2)) (4.6.0)
Requirement already satisfied: MarkupSafe>=0.23 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from Jinja2>=2.10.1->Flask->-r requirements.txt (line 1)) (1.1.1)
Requirement already satisfied: python-engineio>=3.13.0 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from python-socketio>=4.3.0->Flask-SocketIO->-r requirements.txt (line 2)) (3.13.0)
Requirement already satisfied: six>=1.9.0 in /Users/macone/Library/Python/3.7/lib/python/site-packages (from python-socketio>=4.3.0->Flask-SocketIO->-r requirements.txt (line 2)) (1.13.0)

settings.json (.vscode/settings.json)

{
"kite.showWelcomeNotificationOnStartup": false,
"workbench.activityBar.visible": false,
"explorer.confirmDelete": false,
"workbench.colorTheme": "Expressive Theme",
"workbench.iconTheme": "vscode-icons",
"css.remoteStyleSheets": [
"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
],
"git.confirmSync": false,
"python.jediEnabled": false,
"markdown-preview-enhanced.liveUpdate": true,
"window.zoomLevel": 1,
"C_Cpp.updateChannel": "Insiders",
"files.associations": {
"*.rmd": "rmd"
// "*.html": "jinja-html"
},
"files.autoSave": "afterDelay",
"explorer.confirmDragAndDrop": false,
"editor.formatOnSave": true,
"liveServer.settings.donotVerifyTags": true,
"liveServer.settings.donotShowInfoMsg": true,
"python.dataScience.sendSelectionToInteractiveWindow": false,
"python.languageServer": "Microsoft",
"latex-workshop.message.update.show": false,
"terminal.integrated.fontFamily": "Menlo for Powerline"
}

I do not know how to provide the content of the Python output panel, I provide the list of python interpreter as provided by score. If it is not what you expect, would you please give some instruction? image

If any worth, here is my .zshrc

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/Users/macone/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="af-magic"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
export PATH="$PATH:/Users/macone/Library/Python/3.7/bin"
DEFAULT_USER=$USER

I tried to install django and similar things happens..

~ » pip3 install django                                                                                                                                                1 ↵ macone@Macs-MacBook-Pro
Requirement already satisfied: django in ./Library/Python/3.7/lib/python/site-packages (3.0.8)
Requirement already satisfied: sqlparse>=0.2.2 in ./Library/Python/3.7/lib/python/site-packages (from django) (0.3.1)
Requirement already satisfied: asgiref~=3.2 in ./Library/Python/3.7/lib/python/site-packages (from django) (3.2.10)
Requirement already satisfied: pytz in ./Library/Python/3.7/lib/python/site-packages (from django) (2020.1)
You are using pip version 19.0.3, however version 20.2b1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~ » which django                                                                                                                                                           macone@Macs-MacBook-Pro
django not found

here is my pip3 directory and pip3 freeze

~ » where pip3                                                                                                                                                         1 ↵ macone@Macs-MacBook-Pro
/usr/local/bin/pip3
/usr/bin/pip3
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~ » pip3 freeze                                                                                                                                                            macone@Macs-MacBook-Pro
appdirs==1.4.4
asgiref==3.2.10
astroid==2.3.3
attrs==19.3.0
autopep8==1.5.2
cachelib==0.1
certifi==2020.4.5.1
cffi==1.14.0
chardet==3.0.4
click==7.1.2
cryptography==2.9.2
cycler==0.10.0
distlib==0.3.0
Django==3.0.8
entrypoints==0.3
filelock==3.0.12
flake8==3.7.9
Flask==1.1.2
Flask-Session==0.3.2
Flask-SocketIO==4.3.0
gunicorn==20.0.4
idna==2.9
importlib-metadata==1.6.0
isort==4.3.21
itsdangerous==1.1.0
jellyfish==0.8.2
Jinja2==2.11.2
lazy-object-proxy==1.4.3
lib50==2.0.8
MarkupSafe==1.1.1
matplotlib==3.1.2
mccabe==0.6.1
numpy==1.17.4
pexpect==4.8.0
pipenv==2018.11.26
psycopg2-binary==2.8.5
ptyprocess==0.6.0
pycodestyle==2.5.0
pycparser==2.20
pydocstyle==4.0.1
pyflakes==2.1.1
pylama==7.7.1
pylint==2.4.4
pyparsing==2.4.5
python-dateutil==2.8.1
python-engineio==3.13.0
python-socketio==4.6.0
pytz==2020.1
PyYAML==5.3.1
requests==2.23.0
six==1.13.0
snowballstemmer==2.0.0
SQLAlchemy==1.3.17
sqlparse==0.3.1
submit50==3.0.2
termcolor==1.1.0
typed-ast==1.4.0
urllib3==1.25.9
virtualenv==20.0.21
virtualenv-clone==0.5.4
Werkzeug==1.0.1
wrapt==1.11.2
zipp==3.1.0
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

but this which flask works as expected

~ » which flask                                                                                                                                                        1 ↵ macone@Macs-MacBook-Pro
/usr/local/bin/flask

Thank you!

ericsnowcurrently commented 4 years ago

I do not know how to provide the content of the Python output panel

Open the "View" menu and click on "Output". Then in the output panel click on the pull-down menu on the right (probably says "Tasks"). In that menu select "Python".

ericsnowcurrently commented 4 years ago

From what you've provided, it seems most likely that this is a problem with the language server rather than the Python extension itself. If the content of the "Python" output panel doesn't help then we will send this issue over to the language server team for further help.

brettcannon commented 4 years ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.