简体   繁体   English

VSCode 中的 Python:切换到正确环境的最简单方法是什么?

[英]Python in VSCode: What is the easiest way to switch to the correct environment?

I am trying to use Python3 in VS code and for some reason, the VS terminal only will read my macOS system installed version of Python 2.7.我正在尝试在 VS 代码中使用 Python3,出于某种原因,VS 终端只会读取我的 macOS 系统安装版本的 Python 2.7。 I have several other interpreters for python3 installed, but I cannot get the VS terminal to access them to install modules and run my program.我安装了其他几个 python3 解释器,但我无法让 VS 终端访问它们以安装模块并运行我的程序。

I am using the command palette to select the interpreter and it says "current: /path/to/python3".我正在使用解释器 select 的命令调色板,它显示“当前:/path/to/python3”。

That is all good, but when I go to terminal to update my packages for I get this error that to me indicates that terminal is trying to install the package in the default python 2.7 environment.这一切都很好,但是当我 go 到终端更新我的软件包时,我收到这个错误,对我来说表明终端正在尝试在默认的 Z23EEEB4347BDD26BFC6B7EE79A3B755DDZ 环境中安装 package。 How do I access my python3 environments from this terminal?如何从此终端访问我的 python3 环境? Any help is greatly appreciated!任何帮助是极大的赞赏!

The best way to avoid this kind of trouble for each project/development environment is virtual environments.为每个项目/开发环境避免这种麻烦的最好方法是虚拟环境。 Recomended read: https://docs.python-guide.org/dev/virtualenvs/ You can use PipEnv https://github.com/pypa/pipenv or virtual env https://docs.python.org/3/library/venv.html Recomended read: https://docs.python-guide.org/dev/virtualenvs/ You can use PipEnv https://github.com/pypa/pipenv or virtual env https://docs.python.org/3/library /venv.html

The issue is that when you select a globally-installed interpreter it doesn't change what python points at in the terminal.问题是,当您使用 select 全局安装的解释器时,它不会更改python在终端中指向的内容。 You still need to specify either the full path to the interpreter or at least the major.minor version, eg python3.7 to get the interpreter you want.您仍然需要指定解释器的完整路径或至少指定major.minor 版本,例如python3.7以获得您想要的解释器。

Otherwise you can use the advice provided by @paltaa and create a virtual environment to work from to isolate your work and put it all directly under your control.否则,您可以使用@paltaa 提供的建议并创建一个虚拟环境来工作,以隔离您的工作并将其全部直接置于您的控制之下。

I also find the same problem when I was working on python 3. I Deleted the directory in which python 2.7 was installed.我在处理 python 3 时也发现了同样的问题。我删除了安装 python 2.7 的目录。 System automatically refer to Python 3 after that.之后系统自动参考 Python 3。

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

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