How to activate virtualenv using PowerShell?

Question:

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

PS C:Userspiotr> bitcoin_notificationsactivate.ps1
bitcoin_notificationsactivate.ps1 : ```The module
'bitcoin_notifications' could not be loaded. For more information, run
'Import-Module bitcoin_notifications'.``` At line:1 char:1
+ bitcoin_notificationsactivate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (bitcoin_notificationsactivate.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:Userspiotr> ```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 that?

EDIT:
I was copying the method of creating new venv:

  1. pip install virtualenv
  2. mkdir Environments
  3. cd !$
  4. virtualenv bitcoin_notifications
  5. bitcoin_notificationsactivate.ps1
Asked By: user10732864

||

Answers:

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 method — note that step 5 was missing the dot!):

virtualenv bitcoin_notifications
. .bitcoin_notificationsScriptsactivate.ps1

NB: Your question mentions venv but actually is about virtualenv. Note that these are two similar but different tools. Also, in step 5 you were missing the "Scripts" part. Maybe that was a copy-paste error when writing the question, but make sure to include it.

Answered By: wovano

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:foldernamevirtualenvs> virtualenv molecoder
PS C:foldernamevirtualenvs> Set-ExecutionPolicy Unrestricted -Force

and tried to activate it

PS C:foldernamevirtualenvs> molecoderScriptsacivate

and got the following message

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

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

PS C:foldernamevirtualenvs> molecoderScriptsactivate

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:Userspiotr> bitcoin_notificationsScriptsactivate
Answered By: molecoder