Migrating from PyCharm to VS Code: setting paths - python

I want to switch over to VS Code. I did the following to enable python with the packages I typically use, including a local utilities package on this machine:
Searched for the setting python.pythonPath and made it PycharmProjects/Project/venv/Scripts/python.exe for both this workspace and for my user
Changed my Windows user PYTHONPATH to PycharmProjects/Project/venv/Scripts/python.exe
Tried PycharmProjects/python.exe;local-utilities-directory
However, the folder I'm trying to manually set won't show up as a possible option when I click on my interpreter in the bottom right. Instead I'm defaulting to an older interpreter that doesn't have all the packages I've amassed.
While I can access some packages, while trying to pull in my personal utilities library, pip fails (the term pip is not recognized...)
I see that there are a few other ways to change the python path, however, to my understanding, whether you do it in a .json or via the IDE UI, shouldn't actually matter.
Any help would be appreciated.

So, I think I messed things up by offering too many paths.
I had to double check that the exact same path showed up first in:
Work setting pythonPath
User setting pythonPath
Windows profile path
Windows profile PYTHONPATH
I deleted the other "backup" paths, and it ended up working.

Related

VSCode mouse-hover on Python Keywords not working

While beginning to teach a friend how to code in Python, I observed something in his VSCode that was different than my own - he could mouse-hover on a Python keyword such as as import or def, and it would show the Python Manual definition for the keyword. My own VSCode does not do this.
I have tried everything I can think of to find why this won't work for me, and I'm at a complete loss. I have compared my installation process to my friend's, followed the exact same steps I provided him for setting up his environment, and do not get the same results. I tried completely removing Python, VSCode and all residual VSCode files from my laptop, and reinstalling in the same order he did, still to no avail.
I followed those same steps on a different laptop at work, and I can get the MouseHover to work there. The only feasible difference I can think of is that my work machine is not using the same Windows account as both my home PC and laptop - but I do not have Settings Sync enabled so I can't see why that should matter anyway.
Any thoughts for what I'm overlooking to get this functionality working properly in VSCode for myself? I don't particularly need it, but I'd like to be in the same environment as my friend while I teach him.
We're both on VSCode 1.55.2, Python 3.9.4, using a virtual environment created from VSCode's Powershell terminal, then restarted VSCode to get it to detect the virtual environment and use it as default for Terminals, and finally pip installed and enabled Pylint as the linter.
Editing to add steps followed - I just went through all of this again on my home laptop, after completely removing all traces of VisualStudio2019 and Code, and Python. Note: this was on Windows 10, with all updates applied
Install VSCode
Install VSCode Python extension, MS version
Install Python 3.9.4
Open a new folder in VSCode, then open Terminal
python -m venv venv
create a something.py in the Explorer pane
VSCode should detect the venv and offer to load it. Say yes
VScode may also indiacte that pylint is not installed, Allow it to install, or manually pip install pylint from the venv.
in something.py type out import random
Mouse over import -- this is the discrepancy. On both of my home machines, there is no mousehover anything. on both of my friend's machines, as well as my machine at work, the following is shown:
With help from riov8's comment on the original post, I was able to compare the Experiment Groups that were in effect for each of the environments.
In VSCode, with a Python file loaded, switch to the Output panel (in same area as Terminal) and change the dropdown to Python.
Scroll to the top of this panel, and examine the Experiment Groups you belong to.
Most of the experiment groups do not apply to this issue.
Look specifically for either pythonJediLSP, pythonJediLSPcf or if
you have neither.
If you are experiencing the issue I describe, then at the bottom of this same panel, when you first mouse over a python keyword after starting VSCode, you will get errors similar to the following:
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the line is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
%UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the column is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Deprecated since version 0.17.0. Use the project API instead, which means Script(project=Project(dir, sys_path=sys_path)) instead.
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:626: DeprecationWarning: Deprecated since version 0.16.0. Use Script(...).infer instead.
script.goto_definitions(), request["id"]
The following scenarios resolved the issue for me:
User belongs to experiment group pythonJediLSPcf:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python> Experiments: Opt Out From, click Edit in settings.json
in the section "python.experiments.optOutFrom", add "pythonJediLSPcf" (quoted)
if it does not exist, add another section "python.experiments.optInto"
In the OptInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User does not belong to either pythonJediLSPcf or pythonJediLSP:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python › Experiments: Opt Into, click Edit in settings.json
In the "python.experiments.optInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User belongs to pythonJediLSP:
It was my experience that this was the experiment required in order for the feature to work properly. If you have this experiment enabled already, then ensure you do not also have pythonJediLSPcf. Disable it if you do, following the instructions in the first sub-heading. If it continues to not work, then there may be another conflict that I did not experience myself.
I am posting a GitHub issue regarding this problem and will update this answer with the link when done.

Python -V not working or returning any error on Windows 10

I tried to upgrade python version. It didn't work so I uninstalled python from my system. Deleted from Path and re-installed it. Ever since the reinstall
python -V
doesn't work. I have set the path manually and didn't work. Uninstalled and tried again, this time checked the tick-box that says add python to path.
No matter what I do can't seem to get the commands working.
As like the picture I get nothing. The version or anything else.
How am I to fix this problem?
Reason for the behaviour
Your system is using wrong python.exe. (not the one you installed, but one coming with Windows)
How to fix this?
It's completely ok to keep the C:/Users/<user>/AppData/Local/Microsoft/WindowsApps on you PATH, but make sure that your python installation folder (folder with python.exe) is in your PATH above the C:/Users/<user>/AppData/Local/Microsoft/WindowsApps, because the PATH is checked in order from top to bottom (or left to right).
To edit the environment variables, use Win -> type "envir" to the search -> Edit environment variables for your account -> Environment Variables... -> Select from the top list (user list) the "Path" and press "Edit...".
Notes
You can check what python.exe your command is using in Powershell with Get-Command python.
You probably want to also include the /Scripts folder of your python installation to the PATH. (there are common tools like pip.exe, and any .exe coming from a package, such as ipython.exe)
This problem happens because there is a default python.exein newer Windows 10 versions. See: Who put Python in the Windows 10 May 2019 Update?
The Process PATH is not updated if you edit the User or System PATH; you need to restart the Powershell to make the changes visible. If you use terminal of an IDE, you need to close all windows for the process once.
Delete the file c:/Users/<user>/AppData/Local/Microsoft/WindowsApps/python.exe

Set global path for pylint/pycodestyle in VSCode

I'm trying to set up VSCode to use the user level installed pylint and pycodestyle so that when using a virtual environment I can keep using linting, without actually having it installed in the environment and appearing in the requirements.txt.
According to pip show these are found in /home/alex/.local/lib/python3.8/site-packages however adding
"python.linting.pycodestylePath": "/home/alex/.local/lib/python3.8/site-packages/pycodestyle",
"python.linting.pylintPath": "/home/alex/.local/lib/python3.8/site-packages/pylint/",
To my user settings.json just results in a invalid path error. I can't figure out what to link to here, none of the obvious options (pep8.py or pycodestle.py in that directory, pylint/lint.py, just the pylint directory itself, etc) work for either of them. Can anyone help? I'm using Ubuntu 18.04 if that makes a difference to where they're located.
The path "/home/alex/.local/lib/python3.8/site-packages/ is where the Python code that you installed as a user lives, not where the pylint command lives as the "python.linting.pylintPath" wants. You probably want "/home/alex/.local/bin/pylint`.

Why (or why not) Add Anaconda to path?

I have found a partial answer in this question:
Adding Anaconda to Path or not
But I still don't fully understand. I have had a lot of installation issues when switching from a normal installation Python to Anaconda, requiring me to completely re-install Windows... So i want to get this right now.
What happens internally when I Add Anaconda (or python for that matter) to the PATH? I plan on working with seperate conda environments for different python versions, what could go wrong if I add Anaconda to path in the installation? And what is the difference between doing it in the installation or doing it later through the command prompt? Will it affect my ability to integrate anaconda with PyCharm?
PATH is an environment variable that is a list of locations where executable programs lie (see also the wikipedia page.
Whenever you are in your command line and try to execute some program, for example regedit, then the cmd does not magically know that you mean C:\Windows\regedit.exe. Instead, it searches all locations in your PATH for an executable named regedit and finds it in C:\Windows which is one of the standard parts of PATH in Windows.
That is also, why messing with the PATH can be dangerous if you don't know what you are doing, because it might lead to things not working anymore if, for example you delete parts of the path or add custom directories to it.
That being said, you should now have an idea what happens when you "Add anaconda to path". It simply means, that Anaconda adds the directory where its executables lie to the PATH, hence making it findable when, for example you type conda in your cmd.
That being said, adding Anaconda to PATH is something that is convenient, because the commands can always be found automatically and they will also be found by other programs scanning your PATH for a python executable.
At the same time it is not necessary. When you use e.g. pycharm, then you can specify the path to the interpreter inside of pycharm. it does not necessarily need to be present in your PATH.
Note:
I personally have it on my PATH because I am too lazy to open an Anaconda prompt each time I need it in a cmd and I do not see the harm in it if you understand the consequences and its my only python installation anyway.
Also Helpful:
On windows, you can use the where command to find out from where commands are loaded. For example:
where regedit
gives
C:\Windows\regedit.exe
This can be especially helpful when trying to debug PATH issues
The python.exe of the base environment resides in the
C:\Users\USERNAME\AppData\Local\Anaconda3 folder
If you add this folder to the PATH, you can call that version directly from the prompt and Python will also find many of the installed packages via that anchor folder. However, this is not true for e.g. the Numpy package which heavily depends on compiled C libraries. So you would also need to add the following folders to the PATH:
C:\Users\USERNAME\AppData\Local\Anaconda3\Library\mingw-w64\bin;
C:\Users\USERNAME\AppData\Local\Anaconda3\Library\usr\bin;
C:\Users\USERNAME\AppData\Local\Anaconda3\Library\bin;
C:\Users\USERNAME\AppData\Local\Anaconda3\Scripts;
C:\Users\USERNAME\AppData\Local\Anaconda3\bin;
This is exctly what the activation is for, plus it also gives you the option to easily switch between environments.
Bottom line: Adding Anaconda to the PATH might help in simple cases, but the whole concept of Anaconda's dependency management depends on environments and their activation. It's better to use Anacona the proper way right from the beginning and NOT to add Anaconda to the PATH.

PIP 10.0.1 - Warning "Consider adding this directory to PATH or..."

The script flake8.exe is installed in 'c:\users\me\appdata\local\programs\python\python36-32\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Did some research on this and it seems like some sort of recurring issue.
One fix recommends removing trailing slashes from the environment variable.
Any other ideas?
This occurs every time I install via PIP
I am running python 3.6
If removing trailing slashes from the environment variables don't work, please try the following.
It works fine for me.
I am using window 10 and python3.7.0
Advanced system settings -> Environment Variables -> Path
Select Edit
Select Move Up button for both python and Scripts directory.
Finally your path will be as the following.
If you are using other window version(In the case of not having move up
button), put your python and Scripts directory at the beginning of
the path.
I had the same problem and I found out it was because of installing Python from Microsoft Store, so Uninstall it and download the latest version from python.org
Also, there is an (Add Python to PATH) check box on the installer!
After defining the Path variable, make sure to restart your PC
I wasted a lot of time
Removing trailing back slashes from those path elements is a good idea.
There may also be a problem that pip is written in python which uses case sensitive compares by default, (as is the default on every commonly used platform that I am aware of other than windows). You should also make sure that your path variable matches the case expected by pip, (the actual case of the path elements on windows does not have to match).
To do this go into the control panel, system settings, advanced poperties, advanced, Environmental Variables and edit the path elements that are giving the problem. (Windows has a nasty habit of capitalizing things even when you don't).
Adding a new path solved my problem with this error:
'psycho_django' is not recognized as an internal or external command, operable program or batch file.
Yes, giving full permissions to the "C:\Program Files\Python38-32" all users location was the way to keep it from putting PIP into the Windows User folder AppData\Roaming where it decided to create a new Python\Python38-32 folder since it had no permissions to the main folder.
I fixed this on Windows by adding full control permission to the folder where I installed python.
I installed it in "C:\Program Files\Python38-32".

Categories

Resources