Conda with Cygwin: Environment created but location not found and unusable after activation - cygwin

On Windows 10 I have Miniconda3 installed under C:/home/miniconda3
I'm using Cygwin (bash shell emulator) and within the Cygwin shell (at the command line) I'm trying to create a virtual environment using conda.
I am creating an environment using conda and it appears to be created OK, however when I activate and then try to use the environment I get an error that appears to show that there's something amiss with the environment that's been created. Here's what I've tried so far:
Start with a fresh Miniconda installation (downloaded and installed today), and list the environments:
$ conda env list
# conda environments:
#
base * C:\home\miniconda3
Create an environment:
$ conda create -n test_env
Solving environment: ...working... done
## Package Plan ##
environment location: C:\home\miniconda3\envs\test_env
Proceed ([y]/n)?
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
#
# To activate this environment, use:
# > activate test_env
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#
Display the environments again, showing that the environment was created:
$ conda env list
# conda environments:
#
base * C:\home\miniconda3
test_env C:\home\miniconda3\envs\test_env
Activate the new environment, using source (since Cygwin is running bash). Now when I list the environments I expect to see an asterisk next to the test environment's name, indicating that it's now the active environment, but that's missing:
$ source activate test_env
$ conda env list
# conda environments:
#
base C:\home\miniconda3
test_env C:\home\miniconda3\envs\test_env
At this point I can try using conda commands and they'll all fail since the environment's not being found and/or the environment itself is incomplete/malformed/corrupted:
$ conda list
EnvironmentLocationNotFound: Not a conda environment: C:\home\miniconda3\envs\test_env
$ conda install pandas
EnvironmentLocationNotFound: Not a conda environment: C:\home\miniconda3\envs\test_env
$ conda update pandas
PackageNotInstalledError: Package is not installed in prefix.
prefix: C:\home\miniconda3\envs\test_env
package name: pandas
When I try to deactivate the environment I get the following errors:
$ source deactivate
': not a valid identifierULT_ENV
': not a valid identifier
': not a valid identifierIX
': not a valid identifierPT_MODIFIER
': not a valid identifierON_EXE
Thanks in advance for any suggestions as to what may be going wrong and/or how I can fix this.

Running conda under Cygwin is not fully supported, and the recommended solution for users who want to run conda and associated conda environments in a Linux like shell on Windows is to use the bash shell that comes included with the git client for Windows

Related

Not able to create condo environment in SageMaker studio

Sagemaker studio terminal shows default python version as "Python 3.7.10"
I am trying to create conda environment with python3.9 version as below.
conda create --name custom_python_39 python=3.9
But I get the following error:
RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.
But pip list doesn't show setuptools.
I already tried the following
conda update conda -n base
conda update --force conda
But I get the following erros:
bash-4.2$ conda update conda -n base
Collecting package metadata: done
Solving environment: /
The environment is inconsistent, please check the package plan carefully
The following packages are causing the inconsistency:
- conda-forge/noarch::pyopenssl==22.0.0=pyhd8ed1ab_0
- conda-forge/noarch::requests==2.27.1=pyhd8ed1ab_0
- conda-forge/noarch::urllib3==1.26.9=pyhd8ed1ab_0
- conda-forge/noarch::argon2-cffi==21.3.0=pyhd8ed1ab_0
- defaults/linux-64::argon2-cffi-bindings==21.2.0=py37h7f8727e_0
- conda-forge/noarch::bleach==3.1.4=pyh9f0ad1d_0
- conda-forge/linux-64::brotlipy==0.7.0=py37h5e8e339_1001
- conda-forge/linux-64::conda==4.6.14=py37_0
- conda-forge/linux-64::ipykernel==5.2.0=py37h43977f1_1
- conda-forge/linux-64::ipython==7.13.0=py37hc8dfbb8_2
- conda-forge/linux-64::jedi==0.16.0=py37hc8dfbb8_1
- conda-forge/noarch::jinja2==2.11.3=pyhd8ed1ab_1
- conda-forge/noarch::jsonschema==3.2.0=pyhd8ed1ab_3
- conda-forge/noarch::nbconvert==5.6.1=pyhd8ed1ab_2
- conda-forge/noarch::notebook==6.4.1=pyha770c72_0
- conda-forge/noarch::prompt-toolkit==3.0.5=py_1
- conda-forge/noarch::pygments==2.6.1=py_0
/ Killed

Can not activate a conda environemnt

I want to activate a conda env that I had added before. I can see my condo environment when I run
conda info --envs
base * /Users/myusername/opt/anaconda3
/Users/myusername/opt/miniconda3
/Users/myusername/opt/miniconda3/envs/ML_python_scripts_DiaMOND_env
/Users/myusername/opt/miniconda3/envs/ML_env
/Users/myusername/opt/miniconda3/envs/spyder-env
But when I run
conda activate ML_python_scripts_DiaMOND_env
it says it can not find the environment.
Could not find conda environment: ML_python_scripts_DiaMOND_env
You can list all discoverable environments with `conda info --envs`.
it does the same thing with the other environments, it does not matter from any directory I run this commends. I would really appreciate any help.

how to create a venv with a different python version

I have different venvs in my machine in which I have python 3.10.
Now for a specific project, I realised that python 3.10 is not suitable as some libraries are still not compatible. Therefore when creating a new venv for a new project, I would like to downgrade python, say to 3.8, only for this specific venv.
How can I do that?
What should I type onto the terminal to do this?
PS: I use VS and its terminal to create venv
The recommended way by python.org
The recommended way of managing virtual environments since Python 3.5 is with the venv module within the Python Standard Library itself.
Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments
That is not the same as virtualenv, which is a third party package, outside the Python Standard Library.
Source: https://pypi.org/project/virtualenv/
Dangerous to downgrade (and to upgrade)
Depending on if your system itself uses Python, it could be dangerous for system stability to change the system Python version. Your system might need exactly that version of Python. That is true with Ubuntu.
Install another version of Python
Safer than downgrading or upgrading is installing other versions of Python on the same system.
For example, in Ubuntu 20.04, to install Python 3.9:
# Update package lists
me#mydevice:~$ sudo apt update
# Add the deadsnakes repository
me#mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa
# Install Python 3.9
me#mydevice:~$ sudo apt install python3.9
Install the venv package and create a venv virtual environment
# Install the venv package for Python 3.9
me#mydevice:~$ sudo apt install python3.9-venv
# Make a folder for venv virtual environments
me#mydevice:~$ mkdir ~/.venvs
# Create a new venv virtual environment with Python 3.9 in it
me#mydevice:~$ python3.9 -m venv ~/.venvs/my-venv-name
# Activate the new venv
me#mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me#mydevice:~$
Check versions within the venv virtual environment
# Check the Python version inside the venv:
(my-venv-name) me#mydevice:~$ python -V
Python 3.9.9
# Check the Pip version inside the venv:
(my-venv-name) me#mydevice:~$ pip3 --version
pip 21.2.4 from /home/me/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)
Deactivate the venv virtual environment
(my-venv-name) me#mydevice:~$ deactivate
me#mydevice:~$
Check versions outside any virtual environments
# Check Python version:
me#mydevice:~$ python -V
Python 3.8.10
# Check the Pip version:
me#mydevice:~$ pip3 --version
pip 20.0.2 from /home/me/.venvs/my-venv-name/lib/python3.8/site-packages/pip (python 3.8)
Install more Python versions
To install more Python versions, just change the version number from 3.9 to which ever version you choose, that is available from the deadsnakes repository.
I believe the best way to work with different python versions in isolation is pyenv, managing virtual environments can be done with pyenv-virtualenv.
I think this article from Real Python does a good job at explaining how to manage different python versions as well as different virtual environments.
For posterity, with the tools mentioned above you can do the following (once the proper python versions are installed)
pyenv virtualenv <python_version> <environment_name>
# Then activate it
pyenv local <environment_name>
Now that you've created a virtual environment in the folder, it should be picked up any time you enter the folder. VSCode should also pick it up, as per its documentation.
P.S: The reason I think it's a good approach it's because it allows you to manage python versions as well as environments with a single tool. Each version is installed only once, in one place, which should help because it reduces complexity.
Simple and recent
Supposed that you have a different version of Python installed in your system. To check use the following command to check:
> py --list
-3.10-64 *
-3.7-64
And you want to create a new virtual environment for python 3.7 on a 'test_env' directory. Run the following command:
> py -3.7 -m venv test_env
Then activate the test_env by running the following command on Windows PowerShell:
> .\test_env\Scripts\Activate.ps1
Or Linux:
$ source test_env/bin/activate
Check:
python --version
Python 3.7.0
You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:
virtualenv -p <path-to-new-python-installation> <new-venv-name>
Example:
virtualenv -p C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38
This will create a virtual environment called venv38 with Python 3.8.
you can do it by using "virtualenv" library. It can be installed with command pip install virtualenv
then followed by command
virtualenv "name_of_your_environment" #no quotes
and then use the following code to activate your venv
"name_of_your_environment"\Scripts\activate #NOTE YOU MUST BE at your directory where you created your env.
its for VS CODE but I prefer installing conda and then creating env on conda prompt using conda which later you can access to vs code to and its easy to activate that env from anywhere just type conda activate 'name_of_your_env' on vs terminal
You can use anaconda:
conda create -n mypython3.8 python=3.8
For merely the sake of enough porridge in your stew.

How to create new Anaconda Environments within JupyterHub and bind a Environment to a new Jupyter- Notebook?

I installed JupyterHub using Docker container.
When I log in the web interface, I see the following environment options:
After a while of loading, I see the Launcher tab as follows:
Now, I would like to create some new Anaconda Environments (MyDevEnvPy3.7, MyTestEnvPy3.7, MyDevEnvPy3.8, MyTestEnvPy3.8), and start a new JupyterNotebook that uses exactly one of these Anaconda Environments mentioned above.
The idea is, that I'd be able to configure each Anaconda Environment with different packages and python versions (i.e. 3.7 or 3.8).
Especially in the case, if I need to upgrade some Python packages, i.e. in my MyDevEnvPy3.7, I would like to upgrade the Python packages first in MyTestEnvPy3.7, to see if my code is still running. Then I would like to do like this using MyDevEnvPy3.7.
How can I do this?
Or is it even possible?
I've only worked with the Anaconda Navigator So far. Anaconda Navigator was my starting point, where I configured my Environments in the "Environment" tab first, and then switched to the "Home" tab, selected the Anaconda Environment to be used and launched the Jupyter Notebook (using this Environment). But now in JupyterHub, I don't have a clue, what's the best practice to start a Jupyter Notebook using a specific Anaconda Environment ...
This is pretty much a standard functionality provided by Jupyter/IPython, which technically is called 'adding new kernel'. Please follow these steps (in a shell terminal):
Say you have a conda env named MyTestEnvPy3.8. Activate this env.
Install ipykernel in the conda env you want to add to Jupyter:
$ conda install ipykernel
Install the kernel
$ python -m ipykernel install --user --name test --display-name "MyTestEnvPy3.8"
You can always change the name (how this kernel appears in your jupyter directory) and display-name (how it appears in the JupyterLab's 'Select Kernel' window).
Check if you installed the kernel successfully:
$ jupyter kernelspec list
And now you can find the kernel you installed in JupyterLab's 'Select Kernel' window. To interface you called 'launcher' is the start window of JupyterLab. JupyterHub hosts multiple Jupyter server, once you launch the JupyterLab from it, you are in the world of JupyterLab!
There's no such thing as 'bind a kernel with the notebook', notebook is just a file format, when you open it in JupyterNotebook or JupyterLab interface, you can switch between the kernels anytime.
I finally managed to create differenct Anaconda Environments:
Open "Terminal" in Launcher tab
Paste in the following commands:
# Creates a new Anaconda Environment called "MyDevEnvPy3.7" using Python Version 3.7
# in silent mode (parameter "-y")
conda create -n MyDevEnvPy3.7 python=3.7 -y
# Activates the new created Anaconda Environment "MyDevEnvPy3.7"
conda activate MyDevEnvPy3.7
# Installs "ipykernel" in active Anaconda Environment "MyDevEnvPy3.7" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyDevEnvPy3.7" in Anaconda Environment "MyDevEnvPy3.7"
python -m ipykernel install --user --name MyDevEnvPy3.7 --display-name="MyDevEnvPy3.7"
# Deactivates the new created Anaconda Environment "MyDevEnvPy3.7"
conda deactivate
# Creates a new Anaconda Environment called "MyTestEnvPy3.7" using Python Version 3.7
# in silent mode (parameter "-y")
conda create -n MyTestEnvPy3.7 python=3.7 -y
# Activates the new created Anaconda Environment "MyTestEnvPy3.7"
conda activate MyTestEnvPy3.7
# Installs "ipykernel" in active Anaconda Environment "MyTestEnvPy3.7" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyTestEnvPy3.7" in Anaconda Environment "MyTestEnvPy3.7"
python -m ipykernel install --user --name MyTestEnvPy3.7 --display-name="MyTestEnvPy3.7"
# Installs "numpy" in active Anaconda Environment "MyTestEnvPy3.7" in silent mode
conda install numpy -y
# Deactivates the new created Anaconda Environment "MyTestEnvPy3.7"
conda deactivate
# Creates a new Anaconda Environment called "MyDevEnvPy3.8" using Python Version 3.8
# in silent mode (parameter "-y")
conda create -n MyDevEnvPy3.8 python=3.8 -y
# Activates the new created Anaconda Environment "MyDevEnvPy3.8"
conda activate MyDevEnvPy3.8
# Installs "ipykernel" in active Anaconda Environment "MyDevEnvPy3.8" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyDevEnvPy3.8" in Anaconda Environment "MyDevEnvPy3.8"
python -m ipykernel install --user --name MyDevEnvPy3.8 --display-name="MyDevEnvPy3.8"
# Deactivates the new created Anaconda Environment "MyDevEnvPy3.8"
conda deactivate
# Creates a new Anaconda Environment called "MyTestEnvPy3.8" using Python Version 3.8
# in silent mode (parameter "-y")
conda create -n MyTestEnvPy3.8 python=3.8 -y
# Activates the new created Anaconda Environment "MyTestEnvPy3.8"
conda activate MyTestEnvPy3.8
# Installs "ipykernel" in active Anaconda Environment "MyTestEnvPy3.8" in silent mode
conda install ipykernel -y
# Installs new Kernel called "MyTestEnvPy3.8" in Anaconda Environment "MyTestEnvPy3.8"
python -m ipykernel install --user --name MyTestEnvPy3.8 --display-name="MyTestEnvPy3.8"
# Installs "numpy" in active Anaconda Environment "MyTestEnvPy3.8" in silent mode
conda install numpy -y
# Deactivates the new created Anaconda Environment "MyTestEnvPy3.8"
conda deactivate
Select "File" --> "New Launcher"
Now, you should see the 4 new Anaconda Environments:
By selecting the desired Anaconda Environment in the Notebook section, a new Jupyter Notebook will be launched, and is bound to the selected Anaconda Environment.
Let's check this:
As you can see on the upper right side, the current selected Anaconda Environment is MyDevEnvPy3.7. We expect the Python version 3.7, and that numpy can't be imported, as it wasn't installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyTestEnvPy3.7. We expect the Python version 3.7, and that numpy can be imported, as it was installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyDevEnvPy3.8. We expect the Python version 3.8, and that numpy can't be imported, as it wasn't installed in this Anaconda Environment before.
As you can see on the upper right side, the current selected Anaconda Environment is MyTestEnvPy3.8. We expect the Python version 3.8, and that numpy can be imported, as it was installed in this Anaconda Environment before.

Creating a new environment once virtualenv has been installed

I have successuly installed virtualenv using pip install virtualenv. Then i created a new environment under
(base) C:\Users\xxxxxx\PythonScripts\TwitterTutorial>
using the command virtualenv myenv
then as expected a new environment is created under
C:\Users\xxxx\PythonScripts\TwitterTutorial\myenv
The next step is to activate this environment using this command
(base) C:\Users\xxxx\PythonScripts\TwitterTutorial>.\myenv\Scripts\activate
My issue is that the response is as follows:
(myenv) (base) C:\Users\XXXXXX\PythonScripts\TwitterTutorial>
As you can see, the myenv environment is activated on top of the base one. I found that weird because i expected a response similar to this
(myenv) C:\Users\XXXXXX\PythonScripts\TwitterTutorial>
Maybe the command .\myenv\Scripts\activate is wrong?
On a different note, i have created environments in the past and to activate them i usually do conda activate env and it worked successfully for that particular task (not for this one unfortunately)
Many Thanks for your help

Resources