Some cool hacks with the native conda configurations.
miniconda transplant
Motivation: cloned a entire miniconda and transfer it to a new user.
Steps:
- replace all directory in
conda config --show
usingconda config --set %var%name %value%name
, may useconda init
to update. Useconda info | grep %old_dir%
to double check. - Change the directory in
%/old/dir%/miniconda3/etc/profile.d/conda.sh
thensource
theconda.sh
to update env. Although I findconda init
overwrite it immediately. - replace all directory in ~/.bashrc under the block
conda init
Hint: can usesed -i 's@/old/dir/@/new/dir/@g' ~/.bashrc
. Note thatsed
replace pattern can use any delimiter, such as the@
here, not just/
.
And dosource ~/.bashrc
to update
Step 1 is the key, and step 2 and 3 are mainly for switch to the right conda.sh, and they might be rewritten in no time. These steps may need more investigation but they temporary meet my demand. Let me know your comments and advice if find anything wrong or can be improved.
If you also have jupyter
, you may also need to:
jupyter lab --generate-config
andnano ~/.jupyter/%lab_config%
to change.root_dir
,.ip
,.passwd
, etc.
Run Python of Conda with Win Bat
Why not just use the light and pure python IDLE?
The idea
Activate the environment, query the root directory of current python and run its idle within the activated environment, so that the packages are usable.
1 2 3 4 5 6 7 8 9 10 |
activate YOUR_ENV import sys sys.path >>> C:\\Anaconda3\\envs\\YOUR_ENV\\python37.zip cd C:\Anaconda3\envs\YOUR_ENV\Lib\idlelib idle.bat |
The idle.bat has to be run under the CMD with desired environment activated, else most packages will be unavailable.
For a quick start, a bat file can be written as shown below.
Notice that we use call *
to aviod interruption of current .bat file,
for that the activate
and workon
file is also .bat file and running of a new .bat file will interrupt the current .bat process.
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@ echo off echo 正在切换环境:crawler call activate crawler echo 环境切换成功 echo ================ echo python 3.7.10 echo 环境 Env_name: echo crawler echo [1]启动IDLE Run IDLE echo [2]启动pip Run pip echo ================ set /p env_opt=请输入[1]: if %env_opt%==1 goto opt1 if %env_opt%==2 goto opt2 :opt1 echo 正在启动IDLE Running call C:\Anaconda3\envs\crawler\Lib\idlelib\idle.bat echo IDLE开启成功 Succeed pause exit :opt2 echo 正在进入pip路径 Running cd C:\Anaconda3\envs\crawler\Scripts @ cmd.exe |
Reference:
【利用bat切换虚拟环境】
【bat基本语法】
【bat文件中文运行乱码】如果遇到乱码,记事本另存为ANSI编码方可支持中文编译
【.bat进行java-sdk环境切换】 at CSDN.