A Python script that activates the virtualenv and then runs another Python script?

前端 未结 5 992
别跟我提以往
别跟我提以往 2020-12-04 10:19

On Windows Vista, I need a script that starts the activate (to activate the virtualenv) script in:

C:\\Users\\Admin\\Desktop\\venv\\Scripts         


        
相关标签:
5条回答
  • 2020-12-04 10:40

    If you want call virtualenv'ed Python directly you can do something like this:

     C:\Users\Admin\Desktop\venv\Scripts\bin\python.exe manage.py runserver
    

    Double check python.exe location on your virtualenv folder - don't remember how it is out of my head. This Python associates itself with the virtualenv and uses its site-packages by default.

    0 讨论(0)
  • 2020-12-04 10:48

    runserver.bat:

     CALL [your path]\Scripts\activate.bat
     python manage.py runserver
    
    0 讨论(0)
  • 2020-12-04 10:52

    Rather than using strings you can use a caret (^) as described in this question: Long commands split over multiple lines in Windows Vista batch (.bat) file

    E.g.

    cmd /k cd path/to/activate ^
    activate.bat
    pip uninstall --yes package ^
    pip install git+https://git.server.com/user/project@remote/branch ^
    deactivate
    

    will open a venv and uninstall and reinstall a branch of a Git repository. This is a useful pattern for automating deployment of code into a venv.

    0 讨论(0)
  • 2020-12-04 10:54

    I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:

    CALL path\to\base\virtual\environment\Scripts\activate.bat path\to\your\virtual\environment [path\to\your\virtual\environment]python.exe path\to\your\script\yoursript.py

    Without activate.bat nothing works. I was getting an error about mkl-server. This error is described here https://github.com/numpy/numpy/issues/15523. People complained there about conda being broken, i.e. just calling python.exe yoursript.py does not work.

    0 讨论(0)
  • 2020-12-04 10:57

    You can activate your virtualenv and then start server using a bat file. Copy this script in to a file and save it with .bat extension (eg. runserver.bat)

    @echo off
    cmd /k "cd /d C:\Users\Admin\Desktop\venv\Scripts & activate & cd /d    C:\Users\Admin\Desktop\helloworld & python manage.py runserver"
    

    Then you can just run this bat file (just double click) to start the server

    0 讨论(0)
提交回复
热议问题