How to activate a Conda Environment from the "sh" shell (on Nodejs)? - node.js

I can activate the Conda Environment with any problems from the bash shell. I can use this two options on bash:
source activate env_name
. activate env_name
But I am using the method exec of Node to run the activation of the environment. Node uses sh shell in order to run commands. I tried the commands above, but they did not work. I got this error
/bin/sh: 1: /env_name/bin/source: not found
So I had to use this command to run the environment
const child_process = require('child_process')
child_process.exec('bash -c "source activate env_name"')
Is there a better way to do this to make it work on Ubuntu?
I run this on Windows and it is enough to make it work
child_process.exec('activate env_name')

Conda doesn't do well right now (in conda 4.3.x) with shells that aren't "advanced" like bash and zsh. Pure posix dash and the busybox shells, for example, don't work right now.
Good new though... They soon will. Support for a larger variety of shells is a feature of conda 4.4. The base PR for this work was https://github.com/conda/conda/pull/5044, and there are several follow-ons.
Conda 4.4.0 should be released into canary in the next several weeks.

Related

`conda activate <environment>` on Spyder console not working

I have python 3.7.10, conda 4.12.0, and Spyder 5.0.5 installed on Windows 10, and I can see the list of environments with conda env list.
However, when I run conda activate <environment>, CommandNotFoundError appears:
In[1]: conda activate <env>
Note: you may need to restart the kernel to use updated packages.
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
[...]
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I like to change the environment without running Anaconda navigator (or close Spyder every time I change the environment).
Is there a command that I can run on Spyder console to change the virtual environment? Is it also possible to run different environments on different Spyder consoles?
To change environments you will need to change the interpreter preference and restart the console or create a new one. To change the interpreter preference you can go to Tools > Preferences > Python Interprer > Use the following Python interpreter and select there the path for the python.exe of the env you want to use.
For more info regarding how to use existing enviroments with Spyder: https://docs.spyder-ide.org/current/faq.html#using-existing-environment

How to run a bash script from a python file? [duplicate]

This question already has answers here:
Activate conda environment using subprocess
(1 answer)
Conda command working in command prompt but not in bash script
(4 answers)
Closed 1 year ago.
I want to run the following bash script from inside a python script.
condo activate bne
CUDA_VISIBLE_DEVICES=1 python bne.py --model models/BNE_SGsc --fe ./embeddings/BioEmb/Emb_SGsc.txt --fi names.txt --fo output_BNE_SGsc.txt
My python script/function to do this is as follows -
def do_tensorflow_routine(path_name_file):
os.chdir(os.path.join("../","bne_resources/"))
os.system("conda activate bne")
bne_command = "python bne.py --model models/BNE_SGsc --fe ./embeddings/BioEmb/Emb_SGsc.txt --fi names.txt --fo names_bne_SGsc.txt"
subprocess.run(bne_command,shell=True)
pass
However, I am getting the following error -
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I am using a cluster and my system specifications are as follows -
Static hostname: lab1
Icon name: computer-server
Chassis: server
Machine ID: 104265a0ea5b48c1a3c5a9802294af66
Boot ID: 4277780744ae448292d66a9ff39c76e2
Operating System: Ubuntu 20.04.1 LTS
Kernel: Linux 5.8.0-36-generic
Architecture: x86-64
Please let me know if I am missing something. My basic question is how to run another python script from a given python script, especially when we need to activate and deactivate virtual environments, since the parent python script and child python script require 2 different virtual environments due to some dependency conflicts.
Kindly help me.
Thanks in advance,
Megh
If the terminal does not show (base) after running conda activate bne, then try running:
conda init
In the bash script at the top you need the shebang line #!/usr/bin/env bash and env is used for portability for different distributions of Linux.

Starting with anaconda on Ubuntu 18.04

I installed latest version of Anaconda3-2020-02.
I was trying to follow this instruction in order to create environment for running with python==3.6 instead of python==3.7., because I have python 3.6. installed.
So, running the
conda create --name snakes python=3.6
and then activating my environment with conda activate snakes, it enters environment (snakes). However there are no anaconda packages inside like jupyter notebook or others, no anaconda-navigator... So whats the purpose of it and how can I run these programs from environment?
Also, for some reason (when I am not in the environment, just regular bash shell) $PATH is not set up to the /bin directory in anaconda, just to /condabin. Can you explain this also, because I am not able to run nothing except conda command from shell after recommended installation.
If by default after installation environment is not activated, you should activate it by sourcing a file anaconda3/bin/activate. You will see indicator (base) on the left of your bash prompt.
Good thing about anaconda3-2020-02 is that it is not messing with system python, as newest python is being launched inside environment and proper$PATH is set up just inside environment. If only, for some reason, specific version of python is needed, then it makes sense to set it up with this instruction. I would be just using default one with python 3.7 probably from (base) environment.
Keep in mind that by default anaconda components are not being set up inside new environment being created. In order to bring them, for example jupyter, you should run the command like this:
conda create --name snakes python=3.6 jupyter

Is it possible to use pipenv in same shell (not subshell)?

I'm new to pipenv. I'm used to virtualenv, where I can source a script to "activate" the env in my current shell. Is there a way to use pipenv that way, rather than starting a subshell? I.e. source $(pipenv shell-env) or something like that? I'd like to have a linear shell history, not have to double-exit to exit the terminal window, etc.
I'm using python 3.6 and 3.7 on Mac and Windows primarily.
On Linux or macOS:
source "$(pipenv --venv)/bin/activate"
On Windows (using bash; thanks #Nickolay):
source "$(pipenv --venv)/Scripts/activate"

Windows 7: What PATH to set to when using Conda

I've got anaconda installed and was able to create a Python 3.3 environment. I can switch to it and conda info -e shows that I've switched.
However, I'm confused about what to set my PATH variable to. If I hard code it to the exact env then it works, but I thought that the purpose of conda was to be able to switch easily, as well as update and maintain various environments separately.
Perhaps I misunderstood and there's no way around setting my PATH everytime...
In the Windows cmd shell, use the activate and deactivate commands to change the PATH automatically. For instance, if your environment is called python3, run python3 to "activate" (i.e., add to the PATH) the python3 environment. Use deactivate to remove it.

Resources