相关文章推荐
激动的抽屉  ·  2022-04-21 ...·  2 年前    · 
含蓄的瀑布  ·  spring - How to ...·  2 年前    · 
自信的拖把  ·  django百科 3 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a conda environment at the default location for windows, which is C:\ProgramData\Anaconda2\envs\myenv . Also, as recommended, the conda scripts and executables are not in the %PATH% environment variable. I opened a project in pycharm and pointed the python interpreter to C:\ProgramData\Anaconda2\envs\myenv\python.exe and pycharm seems to work well with the environment in the python console, in the run environment, and in debug mode.

However, when opening the terminal the environment is not activated (I made sure that the checkbox for activating the environment is checked). To be clear - when I do the same thing with a virtualenv the terminal does activate the environment without a problem.

Here are a few things I tried and did not work:

  • Copied the activate script from the anaconda folder to the environment folder
  • Copied the activate script from the anaconda folder to the Scripts folder under the environment
  • Copied an activate script from the virtualenv (an identical one for which the environment is activated)
  • Added the anaconda folders to the path
  • None of these worked. I can manually activate the environment without a problem once the terminal is open, but how do I do it automatically?

  • Go to File -> Settings -> Tools -> Terminal .

  • Replace the value in Shell path with cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat your_environment_name .

  • If I installed Anaconda in C:\Anaconda3 and have an environment named myenv , then my settings would look like this:

    Thanks! This is a great workaround, but will not work if I have multiple environments for testing purposes. Also, of course some minor changes are needed for linux/mac pipefish Jul 9, 2018 at 14:22 Do I need to do something more to get this to work? This activates the environment when opening the terminal, but not when trying to use "Run configuration". Daniel Feb 16, 2019 at 10:34 @Daniel You need to configure your project interpreter . Changing the shell path doesn't effect your run/debug configurations, it's a separate setting. darksinge Feb 16, 2019 at 18:57 If I have both conda env and virtualenv env in Pycharm, what should I do, it seems not recognizing the virtualenv env at all. wawawa Aug 26, 2021 at 9:03 it will work! Shell path: powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\tools\miniconda3\shell\condabin\conda-hook.ps1' RedEyed Oct 27, 2021 at 8:53

    If any one wondering for settings for Linux, here is how to do it. Create a file .pycharmrc in your home dir . Open the file and add following

    source ~/.bashrc
    source ~/anaconda3/bin/activate your_env_name 
    

    Now go to Pycharm File > Settings > Tools > Terminal > Shell path replace your shell path with /bin/bash --rcfile ~/.pycharmrc.

    Now when you open your terminal specified conda env will activate.

    Windows & Powershell

    I used partial answers from the answers here and this answer to come up with the following solution on Windows with Powershell

  • Go to File > Settings > Terminal
  • For each project add the following under Environmental variables. Here for each project you need to change venv_name to the name of your virtual environment name.
  • CONDA_ENV=venv_name
  • For the shell path use the following. This you do not need to change for each project, since it uses the environmental variable you declared in the previous step. Just edit the path to your Anaconda/Miniconda installation.
  • powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\path\to\miniconda3\shell\condabin\conda-hook.ps1' ; conda activate $env:CONDA_ENV "
  • If you want to use Powershell on Windows and also want to activate project specific virtual environment immediately, this might be a best option. – su79eu7k Jun 23, 2022 at 14:46

    Experienced the issue on Windows & Pycharm 2022.1 and conda environment My solution is to change default Powershell to cmd shell

  • Open Settings > Tools > Terminal
  • Change Shell path from "powershell.exe" to "cmd.exe"
  • Make sure the "Activate virtualenv" checkbox is checked.
  • New terminal correctly opens with configured environment.

    I had this issue on 2022.3.2 Professional and it worked! Why is this not marked as the accepted solution? – Ignacio Hernández Apr 25 at 9:40
  • For powershell (I recommend this):
    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\tools\miniconda3\shell\condabin\conda-hook.ps1'"
  • For cmd.exe:
    cmd.exe "C:\tools\miniconda3\Scripts\activate.bat"
  • PyCharm will change environment automatically in the terminal
    PS: I'm using my paths to miniconda, so replace it with yours

    This helped me get going. Using this I came up with a general working solution. See below. – Roald Mar 29, 2022 at 8:20 I tried the first solution and didn't worked well in my case, but the second one worked perfectly even when changing the environment in pycharm so thank you very much! – MarinerZZ Aug 19, 2022 at 8:55

    Here's my solution for MacOS or Linux users:

    First, add this to your ~/.bash_profile or ~/.zshrc depends on your shell. Remember to put it after conda has been initialized:

    ##### Activate conda env ######
    [[ -n $CONDA_ENV ]] && conda activate $CONDA_ENV
    

    And then go to your PyCharm settings, go to Tools -> Terminal In Project Settings, add CONDA_ENV=yourenv to your Environment Variables.

    yourenv is the env name from your conda for this specific project.

    This is a great idea, but: (1) First I had to run conda init bash just once (2) I had to prepend conda deactivate before the suggested line, otherwise conda activates the base env by default (3) The -n should not be necessary. – Asclepius Aug 14, 2019 at 19:06 On PowerShell, add conda activate $env:CONDA_ENV to your profile.ps1 to activate the conda environment – tuomastik Jun 2, 2020 at 7:11

    Expanding on darksinge's answer to accommodate for conda>=4.4 and multiple environments, if 1) your conda environments and projects share the same name and 2) you keep your projects in the same directory, you can use this workaround:

    cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat C:\path\to\Anaconda3 & activate %cd:C:\path\to\project\parent\directory\=%

    The last part (%cd:C:\path\to\project\parent\directory\=%) should infer the project name from the current working directory. For example, I keep my projects in Z:\, so %cd:Z:\=% returns my project name. You can read more at: How to replace substrings in windows batch file

    I'm currently searching for a special name/variable in pycharm to use the project name in settings (eg cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat C:\path\to\Anaconda3 & activate $project_name. not found yet – LoneWanderer Mar 14, 2019 at 8:59 Thanks this gave me the idea to try "C:\Program Files\Git\bin\bash.exe" --login && conda activate myenv for Git Bash and it worked! – Justin Harris Mar 8, 2021 at 2:44

    Open up the anaconda prompt and type conda init powershell Notice the highlighted path for the powershell profile (C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1), and copy that.

    retrieving conda-hook powershell profile

    Open up the project in PyCharm and press Ctrl+Alt+S to bring up the settings and navigate to Tools > Terminal. On the right side, in Environment variables text field, set a new environment variable as CONDA_ENV=your_conda_env_name Note that you have to mention your conda environment name correctly. Then in the shell path, type the following.

    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'path/to/conda-hook.ps1' ; conda activate $env:CONDA_ENV "
    

    for example:

    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate $env:CONDA_ENV "
    

    setting the environment vars and shell path

    Apply and close settings, close all the terminals if you have opened any, and try opening a terminal back. The mentioned conda environment should be automatically activated. Cheers!

    Found a solution. Problem is we have been creating conda environments from within Pycharm while starting a new project.

    This is created at the location /Users/<username>/.conda/envs/<env-name>.

    e.g. /Users/taponidhi/.conda/envs/py38.

    Instead create environments from terminal using conda create --name py38. This will create the environment at /opt/anaconda3/envs/.

    After this, when starting a new project, select this environment from existing environments. Everything works fine.

    Mixing a few answers from here, I figured out a solution for Git Bash in Windows:

  • Go to File -> Settings -> Tools -> Terminal.

  • Replace the value in "Shell path" with

    "C:\Program Files\Git\bin\bash.exe" --login && conda activate myenv
    

    Solved for windows users based on the linux solution.

  • Create a batch script (mine named env_win10.bat) in the project root(important) and paste in it the following:
  • @echo OFF
    :: conda activation path
    set conda_activate_path=C:\ProgramData\Anaconda3\Scripts\activate.bat
    :: environments for this project on several of different computers
    set env_path=C:\Users\GiladEiniKbyLake\.conda\envs\bin
    set env_basename=bin
    if exist %env_path% (
        echo activating env: %env_path%
        %conda_activate_path% %env_basename%
    ) else (
        echo cant find path %env_path%
        echo activating base interpreter...
        %conda_activate_path%
    
  • edit in the bat file the paths to:
  • conda_activate_path to the activate.bat of anaconda
  • env_path to the Environment folder
  • env_basename to the Environment name (dont know how to get os.path.basename() on batch script)
  • On pycharm goto Tools->Terminal->shell and enter
  • cmd.exe "/K" ./env_win10.bat
    
  • Bonus:
  • create a shortcut on desktop and insert pycharm exe path and name it in your project name. e.g. wizzi_utils.
  • right click properties and add in the target the path to your project. e.g.
  • "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\bin\pycharm64.exe" "D:\workspace\2021wizzi_utils"
    
  • now your project have a shortcut
  • Notice

    you now have to do it on every project you open in pycharm because pycharm will look for "env_win10.bat" in the root folder of the project. The good news are that you need to do it once per project. It's recommended to put it on ".gitignore", and create this batch script on every user's computer. What if you do want it combined for all users and upload it to git? just add more paths variables. here is an example of a project of mine that i code on my laptop and my desktop:

    @echo OFF
    :: conda activation path
    set conda_activate_path=C:\ProgramData\Anaconda3\Scripts\activate.bat
    :: environments for this project on several of different computers
    set laptop_env_path=C:\Users\gilad\.conda\envs\bin
    set laptop_env_basename=bin
    set desktop_env_path=C:\Users\GiladEiniKbyLake\.conda\envs\bin
    set desktop_env_basename=bin
    :: checking if we are on the 1st option
    if exist %laptop_env_path% (
        echo activating env: %laptop_env_path%
        %conda_activate_path% %laptop_env_basename%
    ) else (
        :: checking if we are on the 2st option
        if exist %desktop_env_path% (
            echo activating env: %desktop_env_path%
            %conda_activate_path% %desktop_env_basename%
        ) else (
            :: fail safe - just activate conda base interpreter
            echo cant locate any of the paths:
            echo    %laptop_env_path%
            echo    %desktop_env_path%
            echo    activating base interpreter...
            %conda_activate_path%
    

    I am using OSX and zshell has become the default shell in 2020. I faced the same problem: my conda environment was not working inside pycharm's terminal.

    File -> Settings -> Tools -> Terminal. the default shell path was configured as /bin/zsh --login

    I tested on a separate OSX terminal that /bin/zsh --login somehow messes up $PATH variable. conda activate keep adding conda env path at the end instead of at the beginning. So the default python (2.7) always took precedence because of messed up PATH string. This issue had nothing to do with pycharm (just how zshell behaved with --login),

    I removed --login part from the script path; just /bin/zsh works (I had to restart pycharm after this change!)

    I ran into the same problem and used this solution.

  • Go to File -> Settings -> Tools -> Terminal.

  • Replace the value in Shell path with

    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& conda activate <yourcondaenvname>

    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Jan 31 at 6:20

    This can happen when you disable activating the (base) environment by default like so:

    conda config --set auto_activate_base false 
    

    To resolve the issue, reactivate the feature.

    conda config --set auto_activate_base true 
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •