How to activate virtualenv using PowerShell?

微笑、不失礼 提交于 2019-12-25 00:07:52

问题


I made virtualenv called bitcoin_notifications.py and I'm going to activate it but:

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

In the result shared before we read the module could not be loaded and if one wants more info to run another specific command.

Once i run it,

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

Import-Module : The specified module 'bitcoin_notifications' was not loaded because no valid module file was found in any module directory.At line:1 char:1 + Import-Module bitcoin_notifications + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (bitcoin_notifications:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId :Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand```

we can understand there's no module in that directory. I just want to activate the virtualenv, how can i do it?


回答1:


I'm not familiar with PowerShell, but the first lines from activate.ps1 seem to mention the solution:

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

So, the following should work (step 4 and 5 from your comment - note that step 5 was missing the dot!):

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

NB: Your question mentions 'venv' but actually is about 'virtualenv'. Note that these are two similar but different tools. When you use the correct terms, it is easier for people to find and answer your question. Also, when including shell commands in your posts, please use the exact same commands that you've used so other people can reproduce the same steps (it looks like this is not the case, since step 5 is missing the "scripts" part). Thanks!




回答2:


I had a very similar problem using Windows 10.

So, initially installed Python 3.7 (adding to Path) and ensured pip was working

PS C:\foldername> pip

Then, ran the following commands to install virtualenv

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

Created a virtualenvs folder and got into it

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

Then, create the virtual environment molecoder

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

and tried to activate it

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

and got the following message

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

In my case was because i wrote acivate instead of activate, so the following modification worked

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

In your case you're trying to activate but activate is inside envname/Scripts , you're going to the wrong location.

To fix it you just need to run

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


来源:https://stackoverflow.com/questions/55734460/how-to-activate-virtualenv-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!