1

I have been following all the steps VSC's tutorial ia giving me but i keep running into this error

py : The term 'py' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling 
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ py -3 -m venv .venv
+ ~~
    + CategoryInfo          : ObjectNotFound: (py:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

.venv\scripts\activate : File C:\Users\natha\.venv\scripts\Activate.ps1 cannot be loaded because running scripts is disabled 
on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:2 char:1
+ .venv\scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

This is the command i am using on windows 11

py -3 -m venv .venv
.venv\scripts\activate

I do not know what's going on because I've been following the tutorial on vs code

1

1 Answer 1

-1

Take a look at this post. With windows, running py or python in a terminal (command prompt, PowerShell, git bash) should work in making a virtual environment. From your post, you are attempting to run this virtual environment in PowerShell. You would need to allow execution of PowerShell scripts (by example mentioned here). In a nutshell, open PowerShell as an Administrator, and run the command set-executionpolicy remotesigned or run set-executionpolicy unrestricted (mentioned here. Now you should be able to make a new virtual environment as so:

PS C:\Users\kyrlon\Desktop> py -m venv env1
PS C:\Users\kyrlon\Desktop> .\env1\Scripts\activate
(env1) PS C:\Users\kyrlon\Desktop> deactivate env1
PS C:\Users\klongwood3\Desktop> py -m venv env1

OR

PS C:\Users\kyrlon\Desktop> python -m venv env1
PS C:\Users\kyrlon\Desktop> .\env1\Scripts\activate
(env1) PS C:\Users\kyrlon\Desktop> deactivate env1
PS C:\Users\klongwood3\Desktop> python -m venv env1

An alternative is to use command prompt as such:

Microsoft Windows [Version 10.0.19044.1889]
(c) Microsoft Corporation. All rights reserved.

C:\Users\kyrlon>py -m venv py_venv
C:\Users\kyrlon>.\py_venv\Scripts\activate.bat
(py_venv) C:\Users\kyrlon>

OR

Microsoft Windows [Version 10.0.19044.1889]
(c) Microsoft Corporation. All rights reserved.

C:\Users\kyrlon>python -m venv py_venv
C:\Users\kyrlon>.\py_venv\Scripts\activate.bat
(py_venv) C:\Users\kyrlon>
3
  • This doesn't address the question. The question is why py doesn't work for OP. Creating the environment is different from activating or deactivating it, and shouldn't require running a Powershell script (venv is a Python module after all). Also, the error is very clearly being reported about py, i.e. the Python Launcher for Windows, not about any given script. Sep 22, 2022 at 0:54
  • I'm not following the confusion you are having with my post. In virtual environments with Powershell, activate correlates to the activate.ps1 script. Here is another SO post for reference if you are not familiar with it.
    – kyrlon
    Sep 22, 2022 at 1:06
  • Yes, I understand that perfectly. But OP is not asking about activate. OP is asking about py itself. Notice that the error message says, not that there is a permissions error running the activate script, but that the script was not found. That happens because the previous line, trying to create the venv, also had an error, resulting from py not being found. Sep 22, 2022 at 1:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.