});

Some cool hacks with the native conda configurations.

miniconda transplant

Motivation: cloned a entire miniconda and transfer it to a new user.

Steps:

  1. replace all directory in
    conda config --show

    using

    conda config --set %var%name %value%name

    , may use

    conda init

    to update. Use

    conda info | grep %old_dir%

    to double check.

  2. Change the directory in
    %/old/dir%/miniconda3/etc/profile.d/conda.sh

    then

    source

    the

    conda.sh

    to update env. Although I find

    conda init

    overwrite it immediately.

  3. replace all directory in ~/.bashrc under the block
    conda init

    Hint: can use

    sed -i 's@/old/dir/@/new/dir/@g' ~/.bashrc

    . Note that

    sed

    replace pattern can use any delimiter, such as the

    @

    here, not just

    /

    .
    And do

    source ~/.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:

  1. jupyter lab --generate-config

    and

    nano ~/.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.

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

@ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.