Presently I'm using Python on a Windows system. I installed Python 3.10 from Anaconda and also the Pycharm IDE. I have ensured that Python is in the correct path in the environment variable. I have also replicated this problem using two different versions of Python, 3.10 and 3.9.
Very simply, in PyCharm, I open a terminal and type
conda install -c numpy numpy.
Then, I write a new "main.py" script. I have one line: "import numpy". I receive the error:
Traceback (most recent call last):
File "C:\Users\---\PycharmProjects\pythonProject3\main.py", line 17, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
What am I doing wrong?
Going on advice from a friend, I created a new PyCharm project sitting not in my user directory but on the C: drive, and got the same error. Finally, when trying to re-install the package using either using either pip or conda, I get this message:
# All requested packages already installed.
You have 2 versions of Python:
Default Python (used everytime you open your command prompt and type python or python3)
Anaconda is installing packages in a virtual environment, using it's own Python (it is located in a different path)
You can see the path of your installed python using python -c "import os, sys; print(os.path.dirname(sys.executable))"
You have 2 Options:
Configure the PyCharm in order to use the anaconda Python. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#view_list
Open a command prompt in the project's folder (you can do it easily using PyCharm). Type conda env list. This will show you all available anaconda virtual environments. Choose 1 of them and type conda activate <env_name>, where <env_name>=the name of the environment. Then, run your program using python <name_of_your_program>
You can see the paths where the anaconda environments and packages are installed using conda info
There main reason for this is
You are running your main.py in different environment rather than where you installed numpy.
If you trying to run it via cmd use this method
Check which environment you are in right now. refer this and
this. But the most easiest way to do this is use where command
in windows cmd. C:\> where python or C:\> where python3. You will
get the path of activated interpreter.
list conda envs - conda env list
activate conda env - conda activate <env name>
then run this command. pip freeze . and check is there numpy in
the list. If not you have to find and activate the environment where
you have installed numpy.
If you want to run it in pycharm
Refer this on how to change pycharm interpreter.
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
Many things can cause this, usually its one of these
You may have to restart your terminal, or IDE if running in there, after installing a package to "refresh" the environmental path
The package is not in the environmental path
I have installed conda environment using mambaforge to use snakemake. I have followed the instructions of Snakemake setup tutorial to install mambaforge. I have created the environment and created a .py file, but when I try to run it, it shows the plain code rather than compiling it. When I went to fix it and to add a Conda environment in my PyCharm interpreter it shows me the following error:
I tried to add the conda executable path by browsing to mambaforge/bin/conda but it still shows me the error.
I also tried to use which conda to find the right path, but rather than giving me a path the following is show, I have tried which conda in conda base environment as well as my setup environment, all it returns is the following:
I am sure that my conda environment is setup as conda --version command shows me the version I have installed.
the conda bin installed with mambaforge is at ~/mambaforge/condabin/conda
I'm not aware of mambaforge conda distribution, I'm using Anaconda / Miniconda distributions with an installed mamba module, so I could create a snakemake environment using terminal (i.e. from command line) and register as Python interpreter in PyCharm Settings as "Conda Environment | Existing environment" (see your first screenshot).
So I suggest you:
check whether "Existing environment" works for you, e.g. is able to find you conda environments (e.g base, or others).
You could set up Anaconda/Miniconda + mamba + configure PyCharm for using Snakemake with the help of my detailed guide that I use for my students' homework on snakemake.
P.S: I always run snakemake scripts using command line, e.g from terminal bundled into PyCharm, or just system terminal app or on remote server. I don't use PyCharm run configurations for that. As for PyCharm, I use it with SnakeCharm plugin to have a good snakemake & python code editing support
I've installed Anaconda and created two extra environments: py3k (which holds Python 3.3) and py34 (which holds Python 3.4). Besides those, I have a default environment named 'root' which the Anaconda installer created by default and which holds Python 2.7. This last one is the default, whenever I launch 'ipython' from the terminal it gives me version 2.7. In order to work with Python 3.4, I need to issue the commands (in the shell)
source activate py34
ipython
which change the default environment to Python 3.4. This works fine, but it's annoying since most of the time I work on Python 3.4, instead of Python 2.7 (which I hold for teaching purposes, it's a rather long story). Anyway, I'll like to know how to change the default environment to Python 3.4, bearing in mind that I don't want to reinstall everything from scratch.
If you just want to temporarily change to another environment, use
source activate environment-name
ETA: This may be deprecated. I believe the current correct command is:
source conda activate environment-name
(you can create environment-name with conda create)
To change permanently, there is no method except creating a startup script that runs the above code.
Typically it's best to just create new environments. However, if you really want to change the Python version in the default environment, you can do so as follows:
First, make sure you have the latest version of conda by running
conda update conda
Then run
conda install python=3.5
This will attempt to update all your packages in your root environment to Python 3 versions. If it is not possible (e.g., because some package is not built for Python 3.5), it will give you an error message indicating which package(s) caused the issue.
If you installed packages with pip, you'll have to reinstall them.
Overview
Some people have multiple Conda environments with different versions of Python for compatibility reasons. In this case, you should activate the desired default environment in the shell initialization file (e.g., .bashrc, .zshrc). With this method, you can preserve the versions of Python you use in your environments.
The following assumes environment_name is the name of your environment
Mac / Linux:
Edit your bash profile so that the last line is conda activate environment_name. In Mac OSX this is ~/.bash_profile, in other environments this may be ~/.bashrc
Example:
Here's how I did it on Mac OSX
Open Terminal and type:
nano ~/.bash_profile
Go to end of file and type the following, where "p3.5" is my environment:
conda activate p3.5
Exit File. Start a new terminal window.
Type the following to see what environment is active
conda info -e
The result shows that I'm using my p3.5 environment by default.
For Windows:
Create a command file (.cmd) with activate environment_name and follow these instructions to have it execute whenever you open a command prompt
Create a batch file command, e.g. "my_conda.cmd", put it in the Application Data folder.
Configure it to be started automatically whenever you open cmd. This setting is in Registry:
key: HKCU\SOFTWARE\Microsoft\Command Processor
value: AutoRun
type: REG_EXPAND_SZ
data: "%AppData%\my_conda.cmd"
from this answer: https://superuser.com/a/302553/143794
Under Linux there is an easier way to set the default environment by modifying ~/.bashrc or ~/.bash_profile
At the end you'll find something like
# added by Anaconda 2.1.0 installer
export PATH="~/anaconda/bin:$PATH"
Replace it with
# set python3 as default
export PATH="~/anaconda/envs/python3/bin:$PATH"
and thats all there is to it.
For windows Anaconda comes with Anaconda Prompt which is a shortcut to cmd and can be used run conda commands without adding anaconda in PATH variable.
Find the location of it, copy and rename the copy (say myenv_prompt). Right click myenv_prompt and select properties in the context menu.
The Target form of Properties window should already be filled with text, something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\
There are three parts of this command 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\Miniconda3\
Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target form something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv
Now myenv_prompt will act as shortcut to start cmd with myenv as the default environment for python. This shortcut you can keep in start menu or pinned in taskbar.
One advantage of this method is that you can create a few shortcuts each having different environment as default environment. Also you can set the default folder by filling Start in form of the Properties window
Hope this helps
PS:It is not required to find Anaconda Prompt and can be done by changing target of any shortcut. But you will require to know path of cmd.exe and activate.bat
Just activate your py34 environment when you load your terminal/shell.
If you use Bash, put the line:
conda activate py34
in your .bash_profile (or .bashrc):
$ echo 'conda activate py34' >> ~/.bash_profile
Every time you run a new terminal, conda environment py34 will be loaded.
The correct answer (as of Dec 2018) is... you can't. Upgrading conda install python=3.6 may work, but it might not if you have packages that are necessary, but cannot be uninstalled.
Anaconda uses a default environment named base and you cannot create a new (e.g. python 3.6) environment with the same name. This is intentional. If you want your base Anaconda to be python 3.6, the right way to do this is to install Anaconda for python 3.6. As a package manager, the goal of Anaconda is to make different environments encapsulated, hence why you must source activate into them and why you can't just quietly switch the base package at will as this could lead to many issues on production systems.
Change permanent
conda install python={version}
Change Temporarily
View your environments
run conda info --envs on your terminal window or an Anconda Prompt
If It doesn't show environment that you want to install
run conda create -n py36 python=3.6 anaconda for python 3.6 change version as your prefer
Activating an environment (use Anaconda prompt)
run activate envnme envnme you can find by this commandconda info --envs as a example when you run conda info --envs it show
base * C:\Users\DulangaHeshan\Anaconda3
py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36
then run activate py36
to check run python --version
In Windows, it is good practice to deactivate one environment before activating another.
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment
If you want Anaconda Navigator to default to Virtual Env you created, go to file > Preference and select default conda env in drop down lint:
If you want Anaconda command automatically opens to virtual env without having to type activate envName, do this:
Right click on conda shortcut > go to properties and change the Target to something like this:
%windir%\System32\cmd.exe "/K" C:\Anaconda\Scripts\activate.bat C:\Anaconda\envs\p37
Optionally you can set your default working dir as well, like I did in snapshop below:
gl
On Windows, create a batch file with the following line in it:
start cmd /k "C:\Anaconda3\Scripts\activate.bat C:\Anaconda3 & activate env"
The first path contained in quotes is the path to the activate.bat file in the Anaconda installation. The path on your system might be different. The name following the activate command of course should be your desired environment name.
Then run the batch file when you need to open an Anaconda prompt.
Here is the solution I found for autoactivating my preferred environment on a Windows 10 system:
Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use.
Go to the start menu, right-click 'Anaconda Prompt' and go to file location.
Create a copy of this shortcut file
Open its properties & change the target to the location of your preferred environment.
Now every time you open anaconda prompt through this shortcut it will automatically load your chosen environment.
activate.py is hardcoded to emit conda activate base\n into your shell profile when you evaluate the shell hook produced by conda shell.zsh hook.
you can suppress this hardcoded "auto-activate base" via:
conda config --set auto_activate_base false
then, in ~/.zshrc, ~/.bashrc or wherever you source your shell profile from, you can append the following (after the conda shell hook) to explicitly activate the environment of your choosing:
conda activate py34
I wasn't satisfied with any of the answers presented here, since activating an environment takes a few seconds on my platform (for whatever reason)
I modified my path variable so that the environment I want as default has priority over the actual default.
In my case I used the following commands to accomplish that for the environment "py35":
setx PATH "%userprofile%\Anaconda3\envs\py35\;%PATH%"
setx PATH "%userprofile%\Anaconda3\envs\py35\Scripts;%PATH%"
to find out where your environment is stored, activate it and enter where python.
I'm not sure yet if this approach has any downsides. Since it also changes the default path of the conda executable. If that should be the case, please comment.
For Jupyter and Windows users, you can change the Target path in your Jupyter Notebook (anaconda3) shortcut from C:\Users\<YourUserName>\anaconda3 to C:\Users\<YourUserName>\anaconda3\envs\<YourEnvironmentName>
you could do the same thing for the Anaconda Prompt..etc.
After changing the path you can check your active environment by opening a terminal in Jupyter and run conda info --envs.
I got this when installing a library using anaconda. My version went from Python 3.* to 2.7 and a lot of my stuff stopped working.
The best solution I found was to first see the most recent version available:
conda search python
Then update to the version you want:
conda install python=3.*.*
Source: http://chris35wills.github.io/conda_python_version/
Other helpful commands:
conda info
python --version
Create a shortcut of anaconda prompt onto desktop or taskbar, and then in the properties of that shortcut make sure u modify the last path in "Target:" to the path of ur environment:
C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO
C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu
preview
and this way u can use that shortcut to open a certain environment when clicking it, you can add it to ur path too and now you'll be able to run it from windows run box by just typing in the name of the shortcut.
Tried both source activate default_3_9 and source conda activate default_3_9
but worked conda activate default_3_9
I'm trying to update Anaconda in order to use Python 3.10.4 and then Spyder 5.3.2. Actually, I wanted to set the Python interpreter used by Pycharm inside the Spyder console but it required the newest Spyder version. I didn't try all the possible solutions (it's pending for me to use the window batch and modifying path solutions given here) but:
Since I couldn't update the Anaconda base due to the well-known error on the "Solving environment". Then Python and Spyder remain the same.
Creating a new env allows to get the last Python and then his newest Spyder version but it doesn't actualize the Anaconda shortcuts and even the Anaconda navigator if you set it to this new env still has some inconsistencies like keeping the older Spyder version in his menu.
Besides, on point 2, changing the shortcuts target path doesn't work for me.
Finally, I create a new shortcut of the Spyder file from the Scripts folder inside the environment directory ( C:\Users<userName>>\Anaconda3\envs<EnvName>\Scripts )
I couldn't use the default Anaconda shortcuts but I have what I wanted and quick access.
Once I try to run python on my conda environment it blcok like this and nothing change:
(python3) user todoapp
$ python
Knowing that python was intalled in my conda env using conda create -n python3 python=3 and I have runned my env using source activate python3. What suprising me is once I run ipythonthis work normally but python no :(.
I have searched in the net but nothing solved my issue. Is theire any option?
You're naming your virtual environment python3?? That's the same alias for actual Python 3 binary file in many operating systems, don't do that.
Create another virtual environment with another name (or clone that env to another name) and move your project files to it. Then remove your python3 env.
See https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html on the related commands and paths.
I am new to programming and would appreciate help.
I had been running code successfully but recently installed conda, which is causing issues.
Sys.executable shows
C:\Users\Ben\AppData\Local\Programs\Python\Python37-32\pythonw.exe
in IDLE and
C:\Users\Ben\AppData\Local\Programs\Python\Python37-32\python.exe in
the command prompt.
However, when I run a script from my desktop calling sys.executable in the command prompt, it shows P:\conda\python.exe. When I run other scripts from files on my desktop it also can no longer import modules, which are stored in the same folder as my original Python installation.
My question is: how can I change the interpreter for the files I am running from my desktop so that they can find the correct modules?
I have read all of StackOverflow's ModuleNotFound and sys.executable related threads but cannot fully understand what is going on - thanks for the help.
create new python environment on conda as follows and run your code on new env:
Actually using conda and conda navigator is the better approach
After conda installation:
- Open up the conda command prompt and create your working environment as follwos
conda create -n mytestenv python=3.7
Activate the environment:
conda activate mytestenv
Now, Run your code.
Later on if you need any module, Install your module using conda command on your selected environment, for example
conda install numpy