python create a virtual environment: bat achieve a key

A Python Project 1.New
2.cd% project.home% change to the project root directory
3. Run setup.bat venv create a virtual environment (note within the network will need to manually run setup.py requirements.txt need to download the next installation package dependency directory (pip download -d ./dependency -r requirements.txt) , the external network directly run)
4. Wait for the installation end of the project to reopen the default environment quoted interpreter no abnormal display, or venv / Scripts / python.exe manual configuration you just created;
5. Note setup.bat and denpendency folders, fit_env_dir.py and requirements.txt have to be under the project root directory

 

setup.bat

@echo off 
echo "start check requirements.txt file"
if exist %cd%\requirements.txt (
   echo "check requirements.txt finish"
) else (
  echo "not exist requirements.txt"
)   
echo "start init env"
SET curdir=%cd%\venv
echo %curdir%
if exist  %curdir% (
 RD /S /q %cd%\venv
 echo "delete old  venv"
 TIMEOUT /T 8
 echo "start create new venv"
 python -m venv ./venv
 TIMEOUT /T 5
 echo "create new venv finish"
) else (
 python -m venv ./venv
 echo "finish create venv"
)
call %cd%\venv\Scripts\activate.bat
echo "source env finish"
echo "pwd is : %cd%"
if exist %cd%\dependency (
 ::"delete old dependency"
 ::RD /S /q %cd%\dependency
 echo "have exist dependency"
 TIMEOUT /T 8
 md dependency
 echo "finish new dependency dir create"
) else (
  md dependency
  echo "create dependency dir finish" 
)
echo "start download pack into dependency"
pip download -d ./dependency -r requirements.txt
pip install --no-index --ignore-installed  --find-links=./dependency -r requirements.txt
echo "install packages flowing:"
pip list 
deactivate
echo "exit env virtual"
echo "wait 10 seconds...."
TIMEOUT /T 5
python -m% cd% \ fit_env_dir.py
echo "pyhton3 fit envdir finish"
TIMEOUT /T 10

3.fit_env_dir.py:

import os,re
def fit_activate_bat():
    with open(os.getcwd()+"\\venv\\Scripts\\activate.bat","r+")as f:
        strings=f.readlines()
        for index,line in enumerate(strings):
          if line.__contains__("VIRTUAL_ENV="):
            res=re.findall('VIRTUAL_ENV=(.*)"',line)[0]
            strings[index]=line.replace(res,os.getcwd()+"\\venv")
    with open(os.getcwd() + "\\venv\\Scripts\\activate.bat", "w+",encoding="utf-8")as h:
        h.writelines(strings)
def fit_Activateps1():
    with open(os.getcwd()+"\\venv\\Scripts\\Activate.ps1","r+",encoding="utf-8")as e:
        lines=e.readlines()
        for index, line in enumerate(lines):
            if line.__contains__("env:VIRTUAL_ENV="):
                res = re.findall('env:VIRTUAL_ENV="(.*)"', line)[0]
                lines[index] = line.replace(res, os.getcwd() + "\\venv")
    with open(os.getcwd() + "\\venv\\Scripts\\Activate.ps1", "w+", encoding="utf-8")as g:
        g.writelines(lines)

def all_fit():
    fit_activate_bat()
    fit_Activateps1()
if __name__ == '__main__':
    all_fit()

  

 

Guess you like

Origin www.cnblogs.com/SunshineKimi/p/11295501.html