【问题标题】:How to activate virtualenv using PowerShell?如何使用 PowerShell 激活 virtualenv?
【发布时间】:2019-09-08 02:35:54
【问题描述】:

我创建了名为 bitcoin_notifications.py 的 virtualenv,我将激活它,但是:

PS C:\Users\piotr> bitcoin_notifications\activate.ps1

bitcoin_notifications\activate.ps1 : The module 'bitcoin_notifications' could not be loaded. For more information, run 'Import-Module bitcoin_notifications'. At line:1 char:1 + bitcoin_notifications\activate.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (bitcoin_notifications\activate.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoLoadModule

在我们读取之前共享的结果中,模块无法加载,如果需要更多信息来运行另一个特定命令。

一旦我运行它,

PS C:\Users\piotr> ```Import-Module bitcoin_notifications

Import-Module : 指定的模块 'bitcoin_notifications' 不是 加载,因为在任何模块中都找不到有效的模块文件 目录。At line:1 char:1 + Import-Module bitcoin_notifications + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (bitcoin_notifications:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId :Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand```

我们可以理解该目录中没有模块。我只想激活virtualenv,怎么办?

【问题讨论】:

  • 错误信息表示找不到bitcoin_notifications 模块。模块是否安装正确?
  • 老实说,我是在复制创建新venv的方法:1.pip install virtualenv2.mkdir Environments3.cd !$4.virtualenv bitcoin_notifications5.bitcoin_notifications\activate.ps1

标签: python powershell virtualenv python-venv


【解决方案1】:

我不熟悉 PowerShell,但 activate.ps1 的第一行似乎提到了解决方案:

# This file must be dot sourced from PoSh; you cannot run it
# directly. Do this: . ./activate.ps1

因此,以下应该可以工作(您评论中的第 4 步和第 5 步 - 请注意第 5 步缺少点!):

virtualenv bitcoin_notifications
. .\bitcoin_notifications\Scripts\activate.ps1

注意:您的问题提到了“venv”,但实际上是关于“virtualenv”。请注意,这是两个相似但不同的工具。当您使用正确的术语时,人们更容易找到并回答您的问题。此外,在您的帖子中包含 shell 命令时,请使用您使用过的完全相同的命令,以便其他人可以重现相同的步骤(看起来情况并非如此,因为第 5 步缺少“脚本”部分) .谢谢!

【讨论】:

  • 我不知道。我知道venv和virtualenv是一样的……
  • 对不起,我错过了一个点,因为我找不到它。 :-)
  • 您用来激活虚拟环境的确切命令是什么?它应该以一个点 ('.') 开头,后跟一个空格 (' ') 和激活脚本的路径(可能是 'bitcoin_notifications\Scripts\activate.ps1',具体取决于您当前的工作目录)。
  • 我现在在:PS C:\Users\piotr> 你能告诉我接下来的步骤吗?我完全是初学者,请:-)
  • 当我输入某事时发生了PS C:\Users\piotr> 'bitcoin_notifications\Scripts\activate.ps1 现在发生了:>>
【解决方案2】:

我在使用 Windows 10 时遇到了非常相似的问题。

所以,最初安装了 Python 3.7(添加到路径)并确保 pip 正常工作

PS C:\foldername> pip

然后,运行以下命令来安装 virtualenv

PS C:\foldername> pip install --upgrade setuptools
PS C:\foldername> pip install ez_setup
PS C:\foldername> pip install virtualenv

创建了一个 virtualenvs 文件夹并进入它

PS C:\foldername> mkdir virtualenvs
PS C:\foldername> cd virtualenvs

然后,创建虚拟环境molecoder

PS C:\foldername\virtualenvs> virtualenv molecoder
PS C:\foldername\virtualenvs> Set-ExecutionPolicy Unrestricted -Force

并尝试激活它

PS C:\foldername\virtualenvs> molecoder\Scripts\acivate

并收到以下消息

molecoder\Scripts\acivate : 模块 'molecoder' 不能 加载。有关更多信息,请运行“导入模块摩尔编码器”。在线:1 字符:1 + 摩尔编码器\脚本\激活 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (molecoder\Scripts\acivate:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoLoadModule

在我的情况下是因为我写的是 acivate 而不是 activate,所以下面的修改起作用了

PS C:\foldername\virtualenvs> molecoder\Scripts\activate

在您的情况下,您尝试激活但激活在 envname/Scripts 内,您将进入错误的位置。

要修复它,你只需要运行

PS C:\Users\piotr> bitcoin_notifications\Scripts\activate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多