Can't access activate, deactivate, or conda inside a conda environment - python

So if I load a conda environment and want to use conda to install a package (or whatever else), I have to specify conda.exe.
What I expect to work:
(randomenv) λ conda --version
The system cannot find the path specified.
What actually works:
(randomenv) λ conda.exe --version
conda 4.5.4
Further complicating things, the following:
(randomenv) λ where conda
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda.bat
C:\ProgramData\Anaconda3\Scripts\conda.exe
Is the first entry supposed to be C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda.exe and the environment got setup wrong or something?
Edit:
We're now in to usage breaking issues. I can't use deactivate, because deactivate doesn't have a .exe extension.
Per usual, where deactivate turns up:
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\deactivate
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\deactivate.bat
C:\ProgramData\Anaconda3\Scripts\deactivate
C:\ProgramData\Anaconda3\Scripts\deactivate.bat
Even more hilariously frustrating:
(randomenv) λ C:\Users\[USERNAME]\AppData\Local\conda\conda\endevs\randomenv\Scripts\deactivate
The system cannot find the path specified.
and then it gets even better:
(randomenv) λ cd C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts
(randomenv) λ ls
2to3.exe* conda* easy_install.exe* epylint-script.py iptest3.exe* ipython-script.py pip-script.py pygmentize-script.py pyreverse.bat symilar.exe*
2to3-script.py conda.bat easy_install-script.py idle.exe* ipython.exe* isort.exe* pydoc.exe* pylint.bat pyreverse.exe* symilar-script.py
activate* deactivate* epylint.bat idle-script.py ipython3.exe* isort-script.py pydoc-script.py pylint.exe* pyreverse-script.py wheel.exe*
activate.bat deactivate.bat epylint.exe* iptest.exe* ipython3-script.py pip.exe* pygmentize.exe* pylint-script.py symilar.bat wheel-script.py
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts
(randomenv) λ deactivate
The system cannot find the path specified.
How can't it find a file that's in the current directory?
Edit2:
Found the following issue on GitHub which says that exiting an environment requires that you activate root. That isn't working either, because activate can't be found just like all the other functions listed above.

This just happened to me. Here is what I found out.
Within the scripts folder of my environment were three scripts:
activate.bat
conda.bat
deactivate.bat
along with three more without the .bat extension that were bash scripts. In each case, all they did was a delegate to the globally installed copy of the same tool using an absolute path name.
When given a command, such as "conda" at the prompt, Windows will search for a .com, .exe or .bat file to execute, taking the first one it finds. Because activating an environment adds it's locations to the head of the path, the first instance windows will find is the .bat file within the environment, which will be executed.
In my case, the .bat files had the wrong absolute path, and were trying to delegate to a global copy that did not exist, hence the error message. Even if you were in the environment script directory, you would still get the message because it's not the .bat file that the message refers to, but the program the .bat file delegates to.
When specifying conda.exe, you are specifically asking for the .exe file, which means the .bat file will not be found and run, and therefore the global copy is found, which of course works.
I fixed it by changing the path inside each of the .bat files to the proper path to the tool, according to the output of where x, which fixed the problem.

I had the same problem. Interestingly, when I set up a fresh environment the activate / deactivate commands work fine.
I compared the script folder for this environment with the broken environment. The broken environment had activate / deactive files in the scripts folder just like yours, but the working environment did not.
By deleting the extraneous scripts (deactivate, conda, activate, .pt-postlink), the broken environment now works again.
I suspect something else I've done corrupted this folder. Possible a package I installed is the culprit? If I figure it out I'll post back.

Related

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

With conda/anaconda should I work in (base) all the time?

This is a total newbie question, but I installed conda along with a bunch of other stuff like Tensorflow with a virtual environment, and now I'm a little confused. If I open a terminal my python is pointing to /usr/bin/python. If I go to the base environment ("conda activate base"), my path points to where I installed anaconda, /usr/local/anaconda2. Does conda expect that I always go into the base environment to use all the anaconda packages, or did I do something weird on my install that my non-environment still points to the original system python? Should I just change my path to point to anaconda? I'm on Ubuntu 18.10.
I am a fan of being explicit -- instead of running python at the command line, I use the full path to the python interpreter when running scripts (e.g. /usr/local/anaconda2/bin/python) -- this way I don't rely on the shell behavior or having to worry about which environment is currently active. Running the interpreter using its full path is no different than just running python while the environment is active.
But that's not everbody's cup of tea... So...
Even if anaconda did not prompt you to update your PATH variable, you can do it explicitly by updating your .bashrc or .bash_profile (or .zshrc or whatever else) by adding this line: PATH=/usr/local/anaconda2/bin${PATH}. Dont forget to re-source the .bashrc or close/reopen your shell for it to take effect.
Conda is capable of creating many virtual environments beyond the base, so if you have more than one and you want to set one as the default, then just update the line above for the appropriate path.

PATH not updated correctly from conda activate in VSCode's terminal

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.

how do you get virtualenv to work in windows

On windows 10, virtualenv installs but that's where it stops. Using the "one basic command", "virtualenv MyPathandFile", only gives the error: "'virtualenv' is not recognized as an internal or external command".
Tried "python virtualenv" or variations of it doesn't work either. These give the message: "no such file or directory" Are there other packages required? It was installed in an administrator cmd window. Tried uninstalling/reinstalling as well.
And finally, is there another, easier way to make virtual environments?
It sounds like it is not in your path. Can you go to your python installation directory and look for a folder called Scripts? There should be a virtualenv.exe script there. If it is there, add the path to that folder to your path environment variable.
You can also try python -m virtualenv and see what that returns.
See my Scripts folder (notice virtualenv at the bottom):

How to set the default python path for anaconda on Linux?

I have installed anaconda on a Linux machine. I noticed that after deactivating the anaconda environment with:
source deactivate
When running:
which python
I get:
/home/user/anaconda/bin/python
Instead of
/usr/bin/python
How can I restore this setting?
The comments somewhat cover the answer to the question, but to clarify:
When you installed Anaconda you must have agreed to have it added to your PATH. You'll want to check in your ~/.bash* files and look for any export PATH= lines to check this. So Anaconda is always on your path. The source deactivate command will only deactivate "sub" Conda environments. It will never remove what is called the "root" Conda environment (the one you originally installed). If you don't want Anaconda on your PATH by default then remove it from your ~/.bash* startup files. Then when you want to use Anaconda you'll need to add it to your PATH. Or just add the specific Conda environment you are interested in to your PATH directly, and don't worry about the activate and deactivate scripts. At their core all they do is modify PATH.
I hope that helps clarify things.
Anaconda comes with its own everything, and they ask if you wish to use their software as a default when you install it by adding their bin first to your PATH variable. If you do that, you can only manually remove it later from .bashrc to undo this action.
I chose not to do it, but i made a shell script to start spyder and use the anaconda distribution when i wish to, without altering my PATH by calling spyder like this from the shell script:
PATH=/home/<... path to where i installed anaconda>/bin:$PATH spyder &
This means that i am adding their distribution's bin to the path only for the extent of running that command (spyder), otherwise my environment is unaffected by anaconda.
If i wish to add things to it, i pass an option to the shell when i source it and that triggers these actions:
PATH=/home/<... path to where i installed anaconda>/bin:$PATH
PS1='\[\033[1;34m\](A)\w:\[\033[0m\] '
so that i see (with colors!) that in this terminal i am using an altered PATH, the one with python3 and such from anaconda, etc... When done, i kill the terminal! :)
To get back to the former default, I comment out the entry setting the pathbin the .bashrc file and get these results:
$ which python
/usr/bin/python
$ python -v
python 2.7.*
If you have installed something, you want to use it before older installs, in general.
Your path is well set: your install is searched before system install.
If you want to use system python, run % /usr/bin/python.
If you don't want anaconda, just trash it (or move it somewhere ouside of path).
This may help:
% which -a python.

Categories

Resources