激活 virtualenv 然后运行另一个 Python 脚本的 Python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30927567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A Python script that activates the virtualenv and then runs another Python script?
提问by ugavetheroses
On Windows Vista, I need a script that starts the activate
(to activate the virtualenv) script in:
在 Windows Vista 上,我需要一个脚本来启动activate
(激活 virtualenv)脚本:
C:\Users\Admin\Desktop\venv\Scripts\
And later, in the virtual environment, starts to the manage.py runserver
in the folder:
后来,在虚拟环境中,开始到manage.py runserver
文件夹中:
C:\Users\Admin\Desktop\helloworld\
How should I do? What modules should I use?
我应该怎么做?我应该使用哪些模块?
采纳答案by Ashish Gupta
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)
您可以激活您的 virtualenv,然后使用 bat 文件启动服务器。将此脚本复制到文件中并使用 .bat 扩展名保存(例如 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
然后你就可以运行这个bat文件(双击)来启动服务器
回答by Mikko Ohtamaa
If you want call virtualenv'ed Python directly you can do something like this:
如果你想直接调用 virtualenv'ed Python 你可以做这样的事情:
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.
仔细检查python.exe
您的 virtualenv 文件夹上的位置 - 不记得我是怎么想的。这个 Python 将自己与 virtualenv 相关联并site-packages
默认使用它。
回答by phil_20686
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
您可以使用插入符号 (^),而不是使用字符串,如以下问题中所述:Windows Vista 批处理 (.bat) 文件中的长命令拆分为多行
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 venvand uninstall and reinstall a branch of a Git repository. This is a useful pattern for automating deployment of code into a venv.
将打开一个venv并卸载并重新安装 Git 存储库的一个分支。这是将代码自动部署到 venv 的有用模式。
回答by Weihui Guo
runserver.bat:
运行服务器.bat:
CALL [your path]\Scripts\activate.bat
python manage.py runserver
回答by Andy
I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:
我在 Windows 上使用 Anaconda 3 和 python 3.7.6。必须在我的 .bat 文件中执行此操作:
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
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.
没有 activate.bat 什么都不起作用。我收到关于 mkl-server 的错误。此错误在此处描述https://github.com/numpy/numpy/issues/15523。人们在那里抱怨 conda 被破坏,即仅调用 python.exe yoursript.py 不起作用。