# 重建Python的virtualenv环境

在最近的一次迁移Docker容器中，意外发现[Docker容器迁移后uWSGI运行报错"ImportError: No module named datetime"](https://github.com/huataihuang/cloud-atlas-draft/tree/6f3204fffc11cf006abd394631e2598d98b415c3/service/nginx/uwsgi_django_no_module_named_datetime/README.md)。参考了网上的一些解决方法，似乎大多是升级操作系统导致的virtualenv破坏，需要重建。

虽然我也很怀疑，毕竟Docker容器号称是完全无感知的轻量级虚拟机，迁移环境前后应该完全一致才对（虽然内核会有区别）。但是实在解决不了，只好尝试重建一次Python virtualenv环境。

* 重新创建虚拟环境

```
mv ~/venv2 ~/venv2.bak

virtualenv venv2
source venv2/bin/activate
```

* 将项目中所有使用的模块导出到`requirements.txt`中然后安装这些模块

```bash
source venv2/bin/activate
cd my_project   # 进入项目目录
pip freeze > requirements.txt
```

然后重新安装需要的依赖

```
pip install -r requirements.txt
```

## 参考

* [Rebuilding a Virtualenv](https://help.pythonanywhere.com/pages/RebuildingVirtualenvs/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/rebuild_virtualenv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
