Skip to content

Activating an environment fails because running powershell scripts is disabled by default on Windows #2559

Closed

Description

@qubitron

Environment data

  • VS Code version: 1.27.1 (user setup)
  • Extension version (available under the Extensions sidebar): 2018.8.0
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7..0
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: N/A

Actual behavior

Creating a new terminal in a fresh Windows install results in a powershell error

PS C:\dev\loadtest> & c:/dev/loadtest/env/Scripts/activate.ps1
& : File C:\dev\loadtest\env\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:1 char:3
+ & c:/dev/loadtest/env/Scripts/activate.ps1
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Expected behavior

I don't need need to disable running scripts in able to use VS code

Steps to reproduce:

  1. Fresh install of windows
  2. install python
  3. create virtual environment
  4. try to activate it in VS Code

Activity

  1. d3r3kk commented on Sep 12, 2018

    @d3r3kk

    Hmmm.. this feels like it might be a VSCode problem.

    To fix the issue, you can simply open a Powershell window as administrator and type the following:

    Set-ExecutionPolicy RemoteSigned

    And hit enter, respond y to any prompts that appear.

  2. d3r3kk commented on Sep 12, 2018

    @d3r3kk

    I take it back - we are running an activate.ps1 file from a .venv!

    Hmmm... not sure what we can do about this from the extension's perspective. Don Jayamanne (@DonJayamanne) can we inject some powershell code prior to running activate.ps1 when we launch a python terminal?

  3. added
    feature-requestRequest for new features or functionality
    and removed
    bugIssue identified by VS Code Team member as probable bug
    on Sep 12, 2018
  4. DonJayamanne commented on Sep 12, 2018

    @DonJayamanne

    can we inject some powershell code prior to running activate.ps1 when we launch a python terminal?

    Yes we can.
    Changing the label, as this isn't a bug. we're trying to improve the UX for users of the extension.

    Derek Keeler (@d3r3kk)
    Your original suggestion works, changing the policy will work, however Dan Taylor (@qubitron) doesn't want to have to do it manually.
    Basically we should do what ever we can to remove/reduce such road blocks.

  5. changed the title [-]activate.ps1 fails because running scripts is disabled by default on Windows[/-] [+]Activating an environment fails because running powershell scripts is disabled by default on Windows[/+] on Sep 12, 2018
  6. d3r3kk commented on Sep 12, 2018

    @d3r3kk

    Yep, that's pretty standard Powershell behaviour/expected workflow on a new machine. Not sure we should usurp that is what I'm getting at.

    However, if we choose to do so, it shouldn't be too difficult to put a quick popup and ask the user if doing so will be ok with them first (much like the powershell cmdlet Set-ExecutionPolicy does).

  7. DonJayamanne commented on Sep 12, 2018

    @DonJayamanne

    However, if we choose to do so, it shouldn't be too difficult to put a quick popup and ask the user if

    Agreed.
    After all, we're executing some script that we have no control over.

    Dan Taylor (@qubitron) thoughts

  8. qubitron commented on Sep 12, 2018

    @qubitron
    Author
    1. This will require elevation to set the execution policy correct? Not all users have admin rights on the system
    2. What if the user doesn't want this execution policy set for security reasons, it's off by default for a reason?

    Why do we even need powershell cmdlet to do this, why can't we run a batch file or something else that doesn't require admin privileges/execution policies to be set?

  9. brettcannon commented on Sep 12, 2018

    @brettcannon
    Member

    It's a PowerShell script because that's how you get the prompt updated to list the virtual environment's name: https://github.com/python/cpython/blob/master/Lib/venv/scripts/nt/Activate.ps1

  10. brettcannon commented on Sep 12, 2018

    @brettcannon
    Member

    The other option any notification could have is offer to switch off the automatic activation on top of providing instructions on how to change the policy.

  11. qubitron commented on Sep 12, 2018

    @qubitron
    Author

    OK, so really this is an issue because we're using a powershell cmd prompt instead of CMD (which is what I use outside of VS Code and just lets me activate without having to set any policies).

    Since I assume we can't change the default from powershell to cmd, detecting the issue and prompting users to set the execution policy (with a button) seems like the right solution.

  12. 70 remaining items

  13. mkoohafkan commented on Aug 18, 2023

    @mkoohafkan

    I think this may actually be a working solution via a terminal profile, since it mimics the specifications of the Anaconda PowerShell Prompt shortcut that gets placed in the start menu when Anaconda is installed. Here I've made a "Conda" profile, but you could also add the "args" entry to your default PowerShell profile.

            "Conda": {
                "source": "PowerShell",
                "icon": "terminal-powershell",
                "overrideName": true,            
                "args": ["-ExecutionPolicy",  "ByPass", "-NoExit", "-Command",  "& 'C:\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Anaconda3' "]
            }

    EDIT We can create a new configuration setting "python.condaHome" : "C:\\Anaconda3" and insert it into the Conda terminal profile arguments via ${config:python.condaHome}.

    This appears to work for non-admin users with "restricted" execution policy.

  14. karrtikr commented on Sep 11, 2023

    @karrtikr

    We have a new activation mechanism #11039 which does not require executing any script, hence does not run into this issue, give it a try:

    • Use latest version of VS Code and Python extension
    • Add the following to User settings and reload window:
      "python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
  15. added
    needs PRReady to be worked on
    and removed
    info-neededIssue requires more information from poster
    triage-neededNeeds assignment to the proper sub-team
    on Sep 12, 2023
  16. Tyriar commented on Sep 26, 2023

    @Tyriar

    Verification covered by #11039 TPI

  17. locked as resolved and limited conversation to collaborators on Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-environmentsFeatures relating to handling interpreter environmentsarea-terminalfeature-requestRequest for new features or functionalityverification-neededVerification of issue is requestedverifiedVerification succeeded

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions