简体   繁体   English

在 Visual Studio Code 中将 Python 包安装到虚拟环境中的问题

[英]Problems installing Python packages into a virtual environment in Visual Studio Code

Although I'm enjoying developing in Python in Visual Code, I'm finding managing virtual environments and packages frustrating, and particularly am struggling with installing packages in the right place.尽管我很享受在 Visual Code 中使用 Python 进行开发,但我发现管理虚拟环境和包令人沮丧,尤其是在正确的位置安装包时遇到了困难。 Here's my sequence of steps, and the problem I then have - I wonder if anyone could kindly tell me where I've gone wrong?这是我的步骤顺序,以及我当时遇到的问题 - 我想知道是否有人可以告诉我我哪里出错了? Or do I really need to include the full Python path?还是我真的需要包含完整的 Python 路径?

So first I create a new virtual environment:所以首先我创建一个新的虚拟环境:

在此处输入图片说明

I can see that this works:我可以看到这有效:

在此处输入图片说明

I then choose to use the Python interpreter in this new virtual environment (I can't quite see why I have to do this - surely this should happen as part of the activation process - but I can live with it):然后我选择在这个新的虚拟环境中使用 Python 解释器(我不太明白为什么我必须这样做 - 当然这应该作为激活过程的一部分发生 - 但我可以忍受它):

在此处输入图片说明

At the bottom left corner of my screen, I get the reassuring fact that I'm using the right Python interpreter:在屏幕的左下角,我得到了一个令人欣慰的事实,即我使用的是正确的 Python 解释器:

在此处输入图片说明

I then install a package (I've chosen requests more or less at random):然后我安装一个包(我或多或少随机选择了请求):

在此处输入图片说明

However, this is going in my default Python location.但是,这是在我的默认 Python 位置进行的。 To get it in my new virtual environment, I seem to have to include the full path to the Python interpreter:为了在我的新虚拟环境中使用它,我似乎必须包含 Python 解释器的完整路径:

在此处输入图片说明

This can't be right, although it does work - I can now see the installed package:这不可能是正确的,尽管它确实有效 - 我现在可以看到已安装的软件包:

在此处输入图片说明

Can anyone help please?有人可以帮忙吗?

Personally I haven't had luck using PowerShell (due to permissions to run PowerShell scripts) so I use Command Prompt in VS Code instead.就个人而言,我没有使用 PowerShell(由于运行 PowerShell 脚本的权限),所以我在 VS Code 中使用命令提示符。

For PowerShell, perhaps activating your environment using Scripts\\activate.ps1 will work instead.对于 PowerShell,或许可以使用 Scripts\\activate.ps1 激活您的环境。 From the docs at: https://docs.python.org/3/library/venv.html来自以下文档: https : //docs.python.org/3/library/venv.html

# PS C:\> <venv>\Scripts\Activate.ps1
StackOverflowExample\Scripts\Activate.ps1

It is a bit confusing in VSCode having an interpreter selected and a different Command Prompt/Power Shell terminal used to install packages into a virtual environment.在 VSCode 中选择了一个解释器和一个不同的命令提示符/Power Shell 终端来将包安装到虚拟环境中,这有点令人困惑。

Another confusing point is running StackOverflowExample\\Scripts\\activate doesn't suggest you are doing anything is wrong.另一个令人困惑的地方是运行StackOverflowExample\\Scripts\\activate并不表明您做任何事情是错误的。

I agree with Jason Cook.我同意杰森库克的观点。 activate.bat used to activate the environment in Cmd, you should take Activate.ps1 instead of activate.bat . activate.bat用于在 Cmd 中激活环境,您应该使用Activate.ps1而不是activate.bat

But you need not activate the environment by yourself.但是您不需要自己激活环境。 The Python extension can choose the right one to activate the environment for you when you create a new terminal.当您创建新终端时,Python 扩展可以选择正确的为您激活环境。

After you select the interpreter, you need to create a new terminal.选择解释器后,您需要创建一个新终端。 In general, we take a shortcut of Ctrl+Shift+` .一般来说,我们采用Ctrl+Shift+`的快捷方式。

And if you want to turn off this function, you can set this in the settings.json:如果你想关闭这个功能,你可以在settings.json中设置:

"python.terminal.activateEnvironment": false,

OK, thanks to Jason/Steven I have finally got the hang of what you should be simple, but isn't.好的,感谢杰森/史蒂文,我终于掌握了你应该简单的东西,但事实并非如此。 Here's what I reckon is the easiest way to create and activate a Python virtual environment in Visual Studio code.这是我认为在 Visual Studio 代码中创建和激活 Python 虚拟环境的最简单方法。 Let's say I start with the Tutorial environment active, and want to create one called ForeignHoliday (we can but dream).假设我从激活的Tutorial环境开始,并想创建一个名为ForeignHoliday (我们只能做梦)。 Start by creating the new environment in the VS Code terminal:首先在 VS Code 终端中创建新环境:

在此处输入图片说明

This creates the environment:这将创建环境:

在此处输入图片说明

However, it doesn't activate it, nor does it change the default Python interpreter to use the one for the new virtual environment.但是,它不会激活它,也不会更改默认 Python 解释器以将其用于新的虚拟环境。 You can do both of these things in one go by choosing an interpreter - click here at the bottom left of the VS Code screen:你可以通过选择一个解释器来一次性完成这两件事——点击 VS Code 屏幕左下角的这里:

在此处输入图片说明

You can now select an interpreter - your new virtual environment (irritatingly) won't be listed yet, so you'll have to find it:您现在可以选择一个解释器 - 您的新虚拟环境(令人恼火)尚未列出,因此您必须找到它:

在此处输入图片说明

Choose to find your interpreter:选择寻找您的口译员:

在此处输入图片说明

Double-click on the Python interpreter in the Scripts folder in your new virtual environment (the pythonw alternative doesn't invoke a terminal window, so most people should avoid this - see this SO article ):双击新虚拟环境中 Scripts 文件夹中的 Python 解释器(pythonw 替代方法不会调用终端窗口,因此大多数人应该避免这种情况 - 请参阅此 SO 文章):

在此处输入图片说明

Now press SHIFT + CTRL + ' to start a new terminal window (NOT just CTRL + ', as this switches you to an existing terminal window).现在按 SHIFT + CTRL + ' 以启动一个新的终端窗口(不仅仅是 CTRL + ',因为这会将您切换到现有的终端窗口)。 You should see this:你应该看到这个:

在此处输入图片说明

You can now install and import packages and they will all be in the right place!您现在可以安装和导入包,它们都会在正确的位置! I wish I'd read this answer a few days ago ...我希望我几天前读过这个答案......

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

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