I am using VS Code for the first time, trying to run a .py file. However it is struggling to find the packages I have in conda (no virtual env, just base).
I have changed the Python interpreter so that it's now using "...64-bit (conda)".
I have also added "python.defaultInterpreterPath": "C:\\Users\\username\\anaconda3\\python.exe" into the JSON settings.
When I then run the file, it finds some packages but not all: it finds os, random and collections but not any others...
Also when I run print(sys.executable) I get "C:\Users\username\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe". I think this should instead be pointing towards the python.exe in anaconda3...
Any assistance with accessing my packages in conda would be appreciated!
As always, read the docs:
https://code.visualstudio.com/docs/python/environments#_create-a-conda-environment
It contains several hints of what can go wrong with conda environments in VSCode.
Most important for you:
A conda environment has to activated before you can use it and obviously this is not happening in your case. The terminal must show a (base) or (Anaconda3) on the prompt. Just pointing to the python.exe is not good enough.
"python.defaultInterpreterPath" only works when you first open your workspace. After you changed it, it will be stored in the database.
You need to make sure you have switched the python interpreter to which you want to use. It would be like this:
Related
I am using VSCodes terminal pane. I activate a conda environment. For some reason, the python command is still set to /usr/bin/python, instead of the correct path to the conda environment.
% conda activate myenv
% which python
/usr/bin/python
The correct anaconda environment directory does seem to be in the $PATH variable, but /usr/bin seems above it in priority.
When I open a standard terminal through the OS, the behavior is as I expect.
% conda activate myenv
% which python
/Users/cpl/anaconda3/envs/myenv/bin/python
Please note: I have already set the VSCode preferences key python.pythonPath to /Users/cpl/anaconda3/envs/myenv/bin/python, and I think that it works correctly. When I run a file through right-clicking and selecting Run Python File In Terminal, the correct python (from the conda environment) is executed. My problem is using the VSCode terminal directly to execute python.
My shell is zsh, and I am using OSX. Any advice?
This behavior is explained in the VSCode docs:
Why are there duplicate paths in the terminal's $PATH environment variable and/or why are they reversed?#
It sounds like VSCode will run your .zshrc twice in MacOS, conflicting with the conda-generated PATH variable definitions.
There are two solutions listed in the link above. The one that works for me is to set the VSCode setting "terminal.integrated.inheritEnv": false. The documentation warns that all of your environmental variables will be stripped if you do this. However, I find I still have my custom variables defined in the .zshrc file.
It is worth noting that recent versions of VSCode will prompt you when it detects you are using a conda environment, and suggests making this change.
I don't use zsh, but I've run into this issue in bash and I believe the cause is the same.
Conda has recently changed the "official" method of activating environments, as described in this issue: https://github.com/Microsoft/vscode-python/issues/1882
Before, you needed to modify your .bashrc/.zshrc to prepend PATH with the directory of conda's activate script, and then activate specific environments by typing source activate name_of_env. VSCode-Python activates conda terminals by sending this command to the shell — with visible echo, like you typed it yourself.
The new method is to source $HOME/anaconda3/etc/profile.d/conda.sh in .bashrc and then activate environments with conda activate name_of_env, which is the behavior you're seeing work correctly in a dedicated terminal. VSCode-Python does not yet support this, and there appear to be issues with cross-platform support on Windows that are complicating the transition.
The best solution for now is to ignore the "correct" method of conda activate and consistently use the older source activate name_of_env, which still works (if your PATH is set to include $HOME/anaconda3/bin).
A dark magic below may work:-
In my Big Sur, I added the following empty entry to my settings.json -- can be found at File(Windows)/Code(Mac)>Preferences>Settings -- click on any link stated "Edit in settings.json"
"terminal.integrated.env.osx": {
"PATH": ""
}
All the best!
If anyone else in the future ends up scratching their heads over this particular problem, I’ve found another culprit:
Terminal>Integrated>Env: Enable Persistent Sessions
I suspect what happens is that after you update system paths, VScode caches the old path in the terminal and persists it. In this case it persists the old python path rather than the new conda one.
Toggling this option off and restarting VSCode clears that cache, and the new path is loaded in. You can also toggle the option back on after you’re done.
I'm having an issue where for some reason the virtual environments that I'm creating are accessing my system-wide installations of Python and pip when they shouldn't be.
Here's my fairly simple workflow just make sure I'm not missing anything obvious (Windows 10, Python 3.8.2):
python -m venv venv
venv\Scripts\activate.bat
My path is now prepended by (venv), as you'd expect. However,
pip list
lists all of the system-wide pip packages I have instead of just the ones that should be in that venv.
pyvenv.cfg indicates that
include-system-site-packages = false
When I open the interpreter using
python
In the virtual environment,
sys.executable
Returns the path on my C drive and
print(pip.__file__)
Does the same. I suspect they should instead be pointing to the interpreter and pip in the virtual environment but don't know how to make that happen.
Edit: 4/27/20, Still dealing with this issue, I have tried:
uninstalling and reinstalling Python, both from python.org and the MS Store
Installing python in a new location
Clearing my user and system environment variables and then adding in
just the ones for Python 3.8.
I'm really at a loss here, would appreciate any help.
To anyone looking at this later on, I eventually realized that the problem was only occurring in a specific folder. Making virtual environments elsewhere on my computer seems to work fine. Why this is the case? I have no idea. I'm curious, but not enough to spend another second on this problem. So there you go.
This happened to me too. My problem was that I created the virtual environment under the directory including the global executable (I mean C:\Program Files\Python39). Once I removed this virtual environment and created a new one somewhere else (for example C:\Python), running python command took the executable in the virtual environment rather than the global one.
I had the same problem too. It might be that the path to your virtual environment has a character with accent in it. This basically means that you will have to alter the \Scripts\activate.bat file.
In order to see if that's the case, use the following procedure:
Activate the virtual environment by running the file \Scrips\activate.bat
Run the following command:
echo %PATH%
There should appear a bunch of paths, being path\to\your\virtual\environment\Scripts the first of them (this is obligatory). Check if this path is exactly the same as it is supposed to be (meaning it should have no "strange" characters instead of characters with accents).
If there are any discrepancies, I suggest you alter your \Scripts\activate.bat according to https://stackoverflow.com/a/22348546/11923771, reopen your Command Prompt and follow steps 1 and 2 again to see if your problem was solved.
I hope my problem is described well enough to be able to be understood.
I am in a course following along trying to create a virtual env on VScode and it keeps failing to create a virtual environment
After typing in pipenv install requests, it tells me that it is using:
/anaconda3/bin/python (3.6.5) to create virtualenv…
After seeing this, I realized that I am running 3.7.4 and it is using 3.6.5 to create the virtual env
the response I'm getting on the VScode terminal is:
[pipenv.exceptions.VirtualenvCreationException]:
/anaconda3/bin/python: No module named virtualenv
I believe this(the path/interpreter being used) is the reason it is failing
What I've tried to do to remedy the problem is,
go to the json settings on VScode and change the path. Did not work
I've tried to look up some youtube videos to see what my options on terminal are to fix/ change the path. Couldn't figure it out. Most likely the answer is in front of me but cant seem to figure it out.
After some googling, I checked to see the response when typing
type -a python
printed back to me is:
python is /anaconda3/bin/python
python is /Users/B/miniconda3/bin/python
python is /usr/bin/python
python is /anaconda3/bin/python
python is /Users/B/miniconda3/bin/python
then in the terminal after changing path settings, typing which python I get back:
/anaconda3/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
$ which python
/anaconda3/bin/python
See the accepted answer for details on how to set the desired version to the PATH variable.
How to add python to PATH variable (Linux)
How to add python to PATH variable (Windows)
The problem of multiple python versions can be solved by setting the desired version as the PYTHONPATH. This way, whenever you try to install modules, create venvs or anything, it will be using the correct version.
I hope this helps/provides value :)
I installed Anaconda using defaults.(i'd post a .png of button i pushed but i don't have the rep for more than two links)
I am following the test drive for Anaconda Test Drive and had a problem with Section #2 - Environments.
Following the provided code I created several environments, switched between them, and then deactivated one.
When i deactivated the environment I went to place where 'conda' is not longer a recognized command.
Anaconda Terminal Session:
i open anaconda terminal and begin in the root directory.
i successfully activate the environment 'snowflakes'.
i issue command 'deactivate' which should return me to root directory.
however, i am now farther up the file structure where 'conda' is not a recognized command. i suppose i can issue 'cd appdata\local\continuum\anaconda2' to solve the problem but i'm thinking i've probably messed something up and would like to fix the problem.
any suggestions what to do?
some things i encountered before posting question:
1. when i installed anaconda there was a checkbox that had something to do with PATH - but anaconda encouraged me NOT to check the box - so i did not.
2. I have also read some posts here discussing that one should have a simple path so I wonder if I should install this someplace other than the default path - which is kinda long as you can see in the terminal session.
any help would be appreciated.
I think I know why the command conda is not being recognized.
When you typed the first time (in your screenshot of the terminal) you were at C:\Users\RAdams.GNSMEM\AppData\Local\Continuum\Anaconda2. Inside this folder you should have the executable conda, so when you called it inside this folder makes sense that it may be executed. However, when you called conda outside of this folder (the second time in your screenshot) you had no executable file named conda to be executed. If you wanted to call the command conda without the need of being in the Anaconda2 folder, you should have made this command be inside your PATH variable. Probably that's what the install manager asked you to do and you refused (maybe it recommended you against it because you're dealing with Windows and it may be somewhat different from what happens in Linux).
EDIT:
After taking another look at your screenshot, I saw that the C:\Users\RAdams.GNSMEM\AppData\Local\Continuum\Anaconda2 was, in fact, your environment. You activated this environment and was in the folder C:\Users\RAdams.GNSMEM. Being in this environment makes you able to call the executable files within it even when outside of it. The thing is that you activated snowflakes (and I think that the other environment was automatically deactivated) after that and then deactivated it. So you had no environments loaded when you called conda the last time. Your environments are pretty OK, I guess.
So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.