Installing Miniconda3 and subsequently creating environment doesn't recognize conda - python

I've created an executable installer using python's subprocess wherein it first does a silent install of miniconda3 and then creates environments from respective YAML files.
Now, After running and after installation is completed, while creating envs it cannot recognize conda right away.
Error Shown: Conda is not recognized as an internal or external command.
After running the installer again, there is no issue as such. Conda command works and Env is created. Basically, conda command is not recognized when run subsequently to the miniconda3 installation. Please help to resolve this via some means that I'm unaware of.
I've tried:
Python Subprocess.run's Shell=True.
Restaring script using os.execv
Refreshing env variables using chocolatey by adding 'refreshenv' mid-script
OS - Windows 10
Command used for silent miniconda3 -
start /wait "" Miniconda3.exe /InstallationType=JustMe /RegisterPython=0 /AddToPath=1 /S /D=C:\ProgramData\Miniconda3
Command for creating env -
conda env create --file env_configs/test_env.yaml
Thanks for going through this post. Any help would be appreciated.

Related

Not able to run python file from conda environment

I am not able to run the python file after activating the conda environment in the folder where the python file is.
Below is the screenshot of the error that i am getting after running the python file:-
Below is the screenshot of the folder in which the conda environment and all the other files including python ones are:-
Using /bin/python3 is using the system-level Python, not the one in the environment. Instead, use plain "python" and let the PATH resolve the environment's Python binary.
Or, use conda run, which I find is preferable for programmatic execution within a Conda environment. See conda run --help.
conda run -n PB_2870 python "/path/to/file/script.py"

Create and activate conda environment with cmake

I have the following in a CMakeLists.txt file. I'm trying to use cmake to check if a conda environment named myenv is installed on the system and activate that environment. If the environment does not exist, then create the environment and activate it. This assumes that conda is already installed via Anaconda (or Miniconda).
# Create and activate a Python environment.
cmake_minimum_required(VERSION 3.18)
# Define the project
project(MyExample)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Make sure Python is installed
find_package(Python REQUIRED)
# Activate conda environment, assume Anaconda or Miniconda is already installed
if(EXISTS /opt/miniconda3/envs/myenv)
execute_process(COMMAND conda activate myenv)
else()
execute_process(COMMAND conda create --yes --quiet --name myenv python)
execute_process(COMMAND conda activate myenv)
endif()
When I run the above cmake file, I get the error:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
However, conda is installed on my system and I can activate the environment manually in the terminal. Why does the environment not get activated via cmake?
"Why does the environment not get activated via cmake?"
The first issue is in the error message, which indicates that the shell that CMake evaluates the execute_process command under doesn't know what conda activate means.
This could be because conda init <shell> has never been run for the particular shell that CMake uses (bash, perhaps?). This answer has some details on what conda init does. It could also be because the Conda initialization code that defines conda activate only gets loaded in interactive shells - which I wouldn't expect CMake to be using. There might be hacky ways to force execution in an interactive shell (bash -l -c 'conda activate foo'), but that doesn't matter because...
Even if the above were working, the procedures here don't make sense: a Conda environment's activation status is scoped to the shell process. I would expect that the (sub)shell dies with the completion of the execute_process. So, even if the activation worked, it wouldn't persist any further in the CMake script.
Discussion
Generally, this CMake script does not seem like a good approach. Tight-coupling the compilation of code to the existence of a particularly-named Conda environment at the user level seems to go against the spirit of CMake, which aims to automate the discovery of software dependencies so that users don't need commonly hardcoded locations. Perhaps it might be worth reassessing what is trying to be accomplished and the strategy to get there.
For example, on Conda Forge, lots of packages compile with CMake, but there CMake is executed in the context of an already activated environment and itself knows nothing about Conda. This makes it so the CMake code is completely agnostic to how its dependencies are provided, which I regard as cleaner engineering.

mambaforge, conda executable path is empty in pycharm interpreter(Linux mint 20)

I have installed conda environment using mambaforge to use snakemake. I have followed the instructions of Snakemake setup tutorial to install mambaforge. I have created the environment and created a .py file, but when I try to run it, it shows the plain code rather than compiling it. When I went to fix it and to add a Conda environment in my PyCharm interpreter it shows me the following error:
I tried to add the conda executable path by browsing to mambaforge/bin/conda but it still shows me the error.
I also tried to use which conda to find the right path, but rather than giving me a path the following is show, I have tried which conda in conda base environment as well as my setup environment, all it returns is the following:
I am sure that my conda environment is setup as conda --version command shows me the version I have installed.
the conda bin installed with mambaforge is at ~/mambaforge/condabin/conda
I'm not aware of mambaforge conda distribution, I'm using Anaconda / Miniconda distributions with an installed mamba module, so I could create a snakemake environment using terminal (i.e. from command line) and register as Python interpreter in PyCharm Settings as "Conda Environment | Existing environment" (see your first screenshot).
So I suggest you:
check whether "Existing environment" works for you, e.g. is able to find you conda environments (e.g base, or others).
You could set up Anaconda/Miniconda + mamba + configure PyCharm for using Snakemake with the help of my detailed guide that I use for my students' homework on snakemake.
P.S: I always run snakemake scripts using command line, e.g from terminal bundled into PyCharm, or just system terminal app or on remote server. I don't use PyCharm run configurations for that. As for PyCharm, I use it with SnakeCharm plugin to have a good snakemake & python code editing support

Sys.executable is wrong - ModuleNotFound error

I am new to programming and would appreciate help.
I had been running code successfully but recently installed conda, which is causing issues.
Sys.executable shows
C:\Users\Ben\AppData\Local\Programs\Python\Python37-32\pythonw.exe
in IDLE and
C:\Users\Ben\AppData\Local\Programs\Python\Python37-32\python.exe in
the command prompt.
However, when I run a script from my desktop calling sys.executable in the command prompt, it shows P:\conda\python.exe. When I run other scripts from files on my desktop it also can no longer import modules, which are stored in the same folder as my original Python installation.
My question is: how can I change the interpreter for the files I am running from my desktop so that they can find the correct modules?
I have read all of StackOverflow's ModuleNotFound and sys.executable related threads but cannot fully understand what is going on - thanks for the help.
create new python environment on conda as follows and run your code on new env:
Actually using conda and conda navigator is the better approach
After conda installation:
- Open up the conda command prompt and create your working environment as follwos
conda create -n mytestenv python=3.7
Activate the environment:
conda activate mytestenv
Now, Run your code.
Later on if you need any module, Install your module using conda command on your selected environment, for example
conda install numpy

Conda command is not recognized on Windows 10

I installed Anaconda 4.4.0 (Python 3.6 version) on Windows 10 by following the instructions here: https://www.continuum.io/downloads. However, when I open the Command prompt window and try to write
conda list
I get the
'conda' command is not recognized...
error.
I tried to run
set PATH=%PATH%;C:\Users\Alex\Anaconda3
but it didn't help. I also read that I might need to edit my .bashrc file, but I don't know how to access this file, and how I should edit it.
In Windows, you will have to set the path to the location where you installed Anaconda3 to.
For me, I installed anaconda3 into C:\Anaconda3. Therefore you need to add C:\Anaconda3 as well as C:\Anaconda3\Scripts\ to your path variable, e.g. set PATH=%PATH%;C:\Anaconda3;C:\Anaconda3\Scripts\.
You can do this via powershell (see above, https://msdn.microsoft.com/en-us/library/windows/desktop/bb776899(v=vs.85).aspx ), or hit the windows key → enter environment → choose from settings → edit environment variables for your account → select Path variable → Edit → New.
To test it, open a new dos shell, and you should be able to use conda commands now. E.g., try conda --version.
Things have been changed after conda 4.6.
Programs "Anaconda Prompt" and "Anaconda Powershell" expose the command conda for you automatically. Find them in your startup menu.
If you don't wanna use the prompts above and try to make conda available in a standard cmd.exe or a standard Powershell. Read the following content.
Expose conda in Every Shell
The purpose of the following content is to make command conda available both in cmd.exe and Powershell on Windows.
If you have already checked "Add Anaconda to my PATH environment variable" during Anaconda installation, skip step 1.
If Anaconda is installed for the current use only, add %USERPROFILE%\Anaconda3\condabin (I mean condabin, not Scripts) into the environment variable PATH (the user one). If Anaconda is installed for all users on your machine, add C:\ProgramData\Anaconda3\condabin into PATH.
How do I set system environment variables on Windows?
Open a new Powershell, run the following command once to initialize conda.
conda init
These steps make sure the conda command is exposed into your cmd.exe and Powershell.
Extended Reading: conda init from Conda 4.6
Caveat: Add the new \path\to\anaconda3\condabin but not \path\to\anaconda3\Scripts into your PATH. This is a big change introduced in conda 4.6.
Activation script initialization fron conda 4.6 release log
Conda 4.6 adds extensive initialization support so that more shells than ever before can use the new conda activate command. For more information, read the output from conda init –help We’re especially excited about this new way of working, because removing the need to modify PATH makes Conda much less disruptive to other software on your system.
In the old days, \path\to\anaconda3\Scripts is the one to be put into your PATH. It exposes command conda and the default Python from "base" environment at the same time.
After conda 4.6, conda related commands are separated into condabin. This makes it possible to expose ONLY command conda without activating the Python from "base" environment.
References
Conda 4.6 Release
How do I prevent Conda from activating the base environment?
When you install anaconda on windows now, it doesn't automatically add Python or Conda.
If you don’t know where your conda and/or python is, you type the following commands into your anaconda prompt
Next, you can add Python and Conda to your path by using the setx command in your command prompt.
Next close that command prompt and open a new one. Congrats you can now use conda and python
Source: https://medium.com/#GalarnykMichael/install-python-on-windows-anaconda-c63c7c3d1444
The newest version of the Anaconda installer for Windows will also install a windows launcher for "Anaconda Prompt" and "Anaconda Powershell Prompt". If you use one of those instead of the regular windows cmd shell, the conda command, python etc. should be available by default in this shell.
If you want to use Anaconda in regular cmd on windows you need to add several paths to your Path env variable.
Those paths are (instead of Anaconda3 the folder may be Anaconda2 depending on the Anaconda version on your PC):
\Users\YOUR_USER\Anaconda3
\Users\YOUR_USER\Anaconda3\Library\mingw-w64\bin
\Users\YOUR_USER\Anaconda3\Library\usr\bin
\Users\YOUR_USER\Anaconda3\Library\bin
\Users\YOUR_USER\Anaconda3\Scripts
\Users\YOUR_USER\Anaconda3\bin
I had also faced the same problem just an hour back. I was trying to install QuTip Quantum Toolbox in Python
Unfortunately, I didn't stumble onto this page in time.
Say you have downloaded Anaconda installer and run it until the end.
Naively, I opened the command prompt in windows 10 and proceded to type the following commands as given in the qutip installation docs.
conda create -n qutip-env
conda config --append channels conda-forge
conda install qutip
But as soon as I typed the first line I got the following response
conda is not recognized as an internal or external command, operable program or batch file
error messsage
I went ahead and tried some other things as seen in this figures
error message
Finally after going through a number conda websites, I understood how one fixes this problem.
Type Anaconda prompt in the search bar at the bottom like this (same place where you hail Cortana)
Anaconda prompt
Once you are here all the conda commands will work as usual
If you have installed Visual studio 2017 (profressional)
The install location:
C:\ProgramData\Anaconda3\Scripts
If you do not want the hassle of putting this in your path environment variable on windows and restarting you can run it by simply:
C:\>"C:\ProgramData\Anaconda3\Scripts\conda.exe" update qt pyqt
You need to add the python.exe in C://.../Anaconda3 installation file as well as C://.../Anaconda3/Scripts to PATH.
First go to your installation directory, in my case it is installed in C://Users/user/Anaconda3 and shift+right click and press "Open command window here" or it might be "Open powershell here", if it is powershell, just write cmd and hit enter to run command window. Then run the following command setx PATH %cd%
Then go to C://Users/user/Anaconda3/Scripts and open the command window there as above, then run the same command "setx PATH %cd%"
To prevent having further issues with SSL you should add all those to Path :
SETX PATH "%PATH%;C:\<path>\Anaconda3;C:\<path>\Anaconda3\Scripts;C:\<path>\Anaconda3\Library\bin"
Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website
case #1
You should set 3 path:
%ANACONDAPATH%;
%ANACONDAPATH%\Scripts;
%ANACONDAPATH%\Library\bin;
It will solve problem:
C:\WINDOWS\system32>conda update conda
Solving environment: failed
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/msys2/noarch/repodata.json.bz2>
Elapsed: -
...
case #2
Also you can use Anaconda Promd (for Win10) instead CLI (cmd.exe)
According to the official documentation, for Windows users there are two environment variable PATHs which can be updated, depending on how your Miniconda/Anaconda distribution has been installed -- one is the SYSTEM PATH, the other is the USER PATH: https://docs.conda.io/projects/conda/en/master/user-guide/tasks/manage-environments.html#activating-an-environment
Basically it says the following: If like the official recommendation you have installed conda for yourself on the user level only, you can add a path similar to C:\Users\<user-name>\miniconda3\Scripts to the user variables.
Please remember to change to Anaconda if you are not using miniconda and update the to your system username in order for this to work.
However, if you have a system level install for all users on the same machine, you should add the path c:\miniconda3\Scripts\ to the system variables.
Also, please run conda init in your PowerShell to have conda working on your PowerShell. If there is a Python version already installed on your PC, you can disable "app execution aliases" in Settings (under "Manage app execution aliases") for this to use only the Python ones on the command line to avoid conflict.
Even I got the same problem when I've first installed Anaconda. It said 'conda' command not found.
So I've just setup two values[added two new paths of Anaconda] system environment variables in the PATH variable which are: C:\Users\mshas\Anaconda2\ & C:\Users\mshas\Anaconda2\Scripts
Lot of people forgot to add the second variable which is "Scripts" just add that then conda command works.

Categories

Resources