问题描述
我在本地机器的虚拟环境中创建了一个 Flask 应用程序,我可以在本地运行它(在 http://localhost:5000
).
I have created a flask application in a virtual environment on my local machine and I could run it locally (at http://localhost:5000
).
然后我将这个项目放在一个仓库中,然后我去我的服务器并 git clone 这个项目.
I then put this project in a repo and I then went to my server and git clone this project.
我的本地机器和我的服务器上的所有文件都是相同的.
All files are identical on my local machine and in my server.
然后我想通过尝试在服务器上测试这个虚拟环境 .venv/bin/activate
I then wanted to test this virtual environment on the server by trying .venv/bin/activate
但是我遇到了错误.它说我没有烧瓶!:
However I ran into an error. It says I do not have flask!:
Traceback (most recent call last):
File "__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
我假设我必须先在虚拟环境中初始化一些东西,比如安装所有的依赖项.还是我必须再次pip install flask
?(这样做会很有趣...)
I am assuming that I have to initialize something in the virtual environment first, like installing all of the dependencies. Or do I have to pip install flask
again? (It would be kind of funny to do that...)
推荐答案
作为一般规则,python 环境不能跨机器移植.
这意味着您不能可靠地期望跨机器移植虚拟环境.如果您在不同的操作系统之间移动内容,则尤其如此.例如,在 Windows 中创建的虚拟环境在 Linux 中将无法运行.
This means that you cannot reliably expect to port the virtual environment across machines. This is especially true if you are moving stuff between different operating systems. For example, a virtual environment created in Windows will not work in Linux.
同样,在 OSX 中创建的虚拟环境在 Linux 中也不起作用.有时,您可以获得 Linux > Linux 兼容性,但这是偶然的,不可依赖.
Similarly, a virtual environment created in OSX will not work in Linux. Sometimes, you can get Linux > Linux compatibility, but this is by chance and not to be relied upon.
原因有很多 - 一些库需要针对本机扩展构建,其他库需要兼容的系统库才能工作,等等.
The reasons are numerous - some libraries need to be built against native extensions, others require compatible system libraries in place to work, etc.
因此,最可靠的工作流程如下:
So, the most reliable workflow is the following:
您可以(但我不建议这样做)将您的虚拟环境与您的项目放在同一目录中.如果这样做,请确保不要将虚拟环境根目录添加到源代码控制系统中.最好将您的虚拟环境与源代码分开(请参阅
virtualenvwrapper
项目一个单独管理虚拟环境的好方法).
You can (but I would recommend against this) put your virtual environment in the same directory as your project. If you do so, make sure you don't add the virtual environment root directory to your source control system. It is best to separate your virtual environments from your source code (see the
virtualenvwrapper
project project for a great way to manage your virtual environments separately).
您应该创建一个需求文件,运行pip freeze >要求.txt
.保持此文件更新并将其添加到您的源代码控制系统.在您的目标系统中,只需创建一个 empty 虚拟环境,然后 pip install -r requirements.txt
以确保正确安装所有需求.这样做将确保也构建和安装任何本机扩展.
You should create a requirements file, by running pip freeze > requirements.txt
. Keep this file updated and add it to your source control system. In your target system, simply create an empty virtual environment and then pip install -r requirements.txt
to make sure all requirements are installed correctly. Doing so will make sure that any native extensions are also built and installed.
这篇关于将虚拟环境项目从本地迁移到服务器(flask 项目),是否需要重新安装依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!