简体   繁体   English

使用不正确的 Python 可执行文件激活虚拟环境

[英]Activated virtual environment using incorrect Python executable

I am working on a project through PyCharm.我正在通过 PyCharm 开展一个项目。 When I started the project, the project interpreter was a newly created virtualenv located in my project folder at /path/to/project_folder/venv and using base interpreter /usr/bin/python3.6 .当我开始项目时,项目解释器是一个新创建的 virtualenv,位于我的项目文件夹中/path/to/project_folder/venv并使用基本解释器/usr/bin/python3.6

When working in PyCharm, the Python Console seems to be using the right venv/Python executable etc. Running os.system("which python") returns /usr/bin/python .在 PyCharm 中工作时,Python 控制台似乎正在使用正确的 venv/Python 可执行文件等。运行os.system("which python")返回/usr/bin/python

Next, I activate this venv through my terminal (on Ubuntu 18.04) using the command source /path/to/project_folder/venv/bin/activate which works fine and shows me that it is activated with a (venv) .接下来,我使用命令source /path/to/project_folder/venv/bin/activate通过我的终端(在 Ubuntu 18.04 上)激活这个 venv,它工作正常并显示它是用(venv)激活的。 However, if I run which python , it returns /home/user/anaconda3/bin/python .但是,如果我运行which python ,它会返回/home/user/anaconda3/bin/python

Why is this occurring?为什么会出现这种情况? How can I access the same Python interpreter from the PyCharm console through my Ubuntu terminal?如何通过我的 Ubuntu 终端从 PyCharm 控制台访问相同的 Python 解释器?

Same happened to me in a specific project.在一个特定的项目中,我也遇到了同样的情况。

Symptoms:症状:

  • No executable taken from the venv;没有从 venv 获取的可执行文件; ie: IE:
  • which python3 from bash is not taken from venv which python3不是取自 venv
  • Python packages are not working from venv Python 包在 venv 中不起作用

Cause:原因:

  • You renamed your project folder您重命名了项目文件夹
  • Virtual environment PATH keeps old path to venv虚拟环境 PATH 保留 venv 的旧路径
  • It fails silently它默默地失败

Solutions:解决方案:

  1. Create a new venv and reinstall requirements, OR创建一个新的 venv 并重新安装要求,或者
  2. Rename folder to its old name将文件夹重命名为其旧名称

Change PATH is not the solution.更改PATH不是解决方案。 venv script creates some other routes in various files. venv脚本在各种文件中创建一些其他路由。

Note: Tested with python3 -m venv venv注意:使用python3 -m venv venv测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM