虚拟环境下降级Python版本

发布于 2025-01-20 13:09:25 字数 261 浏览 2 评论 0原文

关于TensorFlow,我总是遇到相同的错误: modulenotfounderror:no模块名为'tensorflow.contrib'

我实际上是在使用Python版本3.9,但是,在线阅读,似乎版本3.7是可以与TensorFlow版本一起使用的最后一个稳定> 2.0 。

不幸的是,我已经用错误的版本的Python启动了我的项目,我想降级它,我该怎么做?

I am always getting the same error regarding TensorFlow:
ModuleNotFoundError: No module named 'tensorflow.contrib'.

I am actually using Python version 3.9 but, reading online, it seems that version 3.7 is the last stable one that can work with TensorFlow version >2.0.

Unfortunately I have started my project in a venv with the wrong version of Python and I would like to downgrade it, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦幻之岛 2025-01-27 13:09:25

基于上面 @chepner 的评论,由于 venvs 只是目录,您可以保存当前状态并启动一个新的虚拟环境。

# Save current installs
(venv) -> pip freeze -r > requirements.txt

# Shutdown current env
(venv) -> deactivate

# Copy it to keep a backup
-> mv venv venv-3.9

# Ensure you have python3.7
-> python3.7 -V

# Create and activate a 3.7 venv
-> python3.7 -m venv venv-3.7
-> source venv-3.7/bin/activate

# Reinstall previous requirements
(venv-3.7) -> pip install -r requirements.txt

# Install new requirements

希望有帮助!

Building on @chepner's comment above, since venvs are just directories, you can save your current state and start a fresh virtual environment instead.

# Save current installs
(venv) -> pip freeze -r > requirements.txt

# Shutdown current env
(venv) -> deactivate

# Copy it to keep a backup
-> mv venv venv-3.9

# Ensure you have python3.7
-> python3.7 -V

# Create and activate a 3.7 venv
-> python3.7 -m venv venv-3.7
-> source venv-3.7/bin/activate

# Reinstall previous requirements
(venv-3.7) -> pip install -r requirements.txt

# Install new requirements

Hope that helps!

深居我梦 2025-01-27 13:09:25

venv --upgrade 命令,但它主要用于在补丁版本之间进行更新(例如从 3.9.15 到 3.9.16)。

您需要额外的步骤才能在不同的次要版本之间进行更新。让我引导您完成它。

假设您安装了两个版本的 Python:

$ python3.9 --version
Python 3.9.16
$ python3.7 --version
Python 3.7.16

并且您的环境是使用版本 3.9 创建的,并且位于 venv 子目录中:

$ . venv/bin/activate
$ python --version
Python 3.9.16
$ deactivate

使用这些命令从 3.9 降级到 3.7:

$ python3.7 -m venv --upgrade venv
$ cd venv/bin
$ ln -sf python3.7 python
$ ln -sf python3.7 python3
$ rm {python,pip}3.9
$ cd -

结果如下:

$ . venv/bin/activate
$ python --version
Python 3.7.16

There's the venv --upgrade <ENV_DIR> command, but it's mainly intended to update between patch versions (e.g. from 3.9.15 to 3.9.16).

You'll need extra steps to update between different minor versions. Let me walk you through it.

Assuming you have both versions of Python installed:

$ python3.9 --version
Python 3.9.16
$ python3.7 --version
Python 3.7.16

And that your environment was created using version 3.9, and it's in a venv subdirectory:

$ . venv/bin/activate
$ python --version
Python 3.9.16
$ deactivate

Use these commands to downgrade from 3.9 to 3.7:

$ python3.7 -m venv --upgrade venv
$ cd venv/bin
$ ln -sf python3.7 python
$ ln -sf python3.7 python3
$ rm {python,pip}3.9
$ cd -

Here's the result:

$ . venv/bin/activate
$ python --version
Python 3.7.16
三生殊途 2025-01-27 13:09:25

tf.contrib 已从最新版本的 TensorFlow 2.x 并替换为 TF Slim

建议使用升级版本的TensorFlow 受益于 TensorFlow 提供的最新特性和功能。

但是,要降级 python

  1. 您需要卸载现有的 python 版本并重新安装所需的 python 版本并设置环境.

  2. 如果您使用的是 Anaconda IDE,请使用以下命令:

conda search python #检查可用的python版本
conda install python=<版本>;
    
conda create --name ; python=; # 这也创建了虚拟环境
激活 

请查看链接了解更多详细信息。

tf.contrib is deprecated from the latest version of TensorFlow 2.x and is replaced with TF Slim.

It is recommended to use the upgraded version of TensorFlow to have the benefit of the latest features and functionalities provided by TensorFlow.

However, To downgrade python

  1. You need to uninstall the existing python version and re-install the required python version and set up your environment.

  2. If you are using Anaconda IDE, then use the below command:

conda search python  #to check available python version
conda install python=<version>
    
conda create --name <env_name> python=<python_version> # Which also creates virtual environment
activate <env_name>

Please check this link for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文