I use Spyder environment for learning Python. I installed pyperclip module using pip in Anaconda prompt (conda couldn't find it) and everything works just fine in Spyder but when I try to run script using Anaconda Prompt, it cannot find the imported module.
So far, I tried to list modules using conda list (pyperclip is there), changing the PATH variable (variable for my user now points to folder with scripts, system PATH variable to Anaconda3 folder). I found out after running sys.executable in both (Spyder and Anaconda prompt - after going to python first) that Spyder points to C:\MyPath\Anaconda3\pythonw.exe' whereas Anaconda prompt points to C:\MyPath\Anaconda3\python.exe'.
Then I tried to set Spyder's interpreter to C:\...\python.exe but it seems like it ignores it.
I think I'm missing something elementary here.
Related
I located a really weird behavior of Jupyter and caused me to reinstall it but the problem persisted.
I have a Windows machine where I have installed anaconda (the latest version). There are two environments:
(base) Which contains nothing special maybe seaborn pandas etc. and is on python version 3.8.12.
(tf-gpu) Which contains tensorflow-gpu and is on python version 3.9.7.
Mind you, these two environments are created directly from a fresh installation of anaconda, and I didn't touch anything except installing tensorflow-gpu.
When I launch Jupyter through Anaconda, it launches normally on the home folder "C:\User\user" for both environments i.e. correct packages installed and correct version of python on both. I can also launch an instance of Powershell through Anaconda or Windows, (always in home dir), and launch Jupyter both environments behave as they should (packages, python version). So far so good...
The problem starts when I want to launch Jupyter from a directory other than home dir. I keep all my project files in a separate partition (D:), thus I navigate to that directory through a cmd/powershell (launched through Anaconda or Win) and type "jupyter notebook". The notebooks open on that director and:
When the base env is selected the notebook reports a correct name "base"(os.environ['CONDA_DEFAULT_ENV']) and python version 3.8.12 (sys.version_info). Everything behave as base env was setup.
When the tf-gpu env is selected the notebook reports a correct name "tf-gpu" but I get a python version 3.8.12, which is completely wrong! It should be 3.9.7. I can't import tensorflow since I get an error. Essentially I am in the base env without knowing it.
I then check in the same cli window (tf-gpu) that launched jupyter and python --version reports 3.9.7 and of course can import tf run it etc.
Jupyter just reports the environment name but in reality it uses the base environment even though it launched from said env. How can this change?
Why does Jupyter works normally when launched from the user directory?
If this is not solved easily, what is a workaround? Jupyter doesn't recognize symbolic links, and thus I can not navigate to another directory if it is not a subdirectory of home.
Finally, I also tried adding the env as kernel through python -m ipykernel install --user --name tf-gpu --display-name "Tensorflow GPU (tf-gpu)", but it doesn't seem to change a thing.
Update 1:
Of course I activate the tf-gpu through conda activate tf-gpu and check the python version and tensorflow (correct results), the problem is when launching jupyter notebook. The notebook reports the tf-gpu env but wrong python version and tf is missing.
Update 2:
After some searching around it seems that jupyter notebook has much trouble selecting a python version if multiple versions of python are installed in the system (in my case 2, 1 on each environment environments). Which is ridiculous if you think that that's why we are using environments for...
The reason you are having this problem I’m guessing is because your launched jupyter notebook from your conda base environment. You will see a (base) in your comment prompt.
To remedy this, When you use the command prompt to launch, do conda activate tf-gpu before you launch Jupyter notebook, now check if your versions are correct.
What I did and worked was run ipython3 notebook in the environment which launched the notebook server and everything was working properly correct environment name and correct python version and packages (tensorflow is working as it should). This must have updated some configuration files since I can now run jupyter notebook from a different folder and the gpu-tfu env works correctly.
Please remember that these problems were occurring only if I was launching jupyter outside the home directory (C:\User\user) on a different partition. Also keep in mind that this is a Windows installation and more complicated things may be happening with user paths, environments, etc than in linux.
In general I am new to python so keep it easy. I have started my coding in Spyder since it was easy to install libraries with Anaconda.
Now we want to switch to VS Code for version control. I have switched interpreter and python path to Anaconda folder, but if I open python files I have created in Spyder I can't use in VS code, issue always is with importing libraries.
Screenshot
The first rule with Anaconda is to not manually mess around with the paths. Anaconda has a different philosophy: Before you can use Python, you need to activate a conda environment.
c:\> conda activate
(Anaconda3) c:\> python
>>>
VSCode detects conda environments and you just have to select one for your project (Python: Select interpreter). Now open a new terminal and watch VSCode activating the environment for you (like shown above).
However, when you start your script for the first time, VSCode sometimes invokes the interpreter even before the conda activation has finialized. Then simply restart your script.
I have an Anaconda distribution installed on Mac OSX (10.14.2). With it, I installed VSCode which I'm to write python scripts. Recently, I've updated some environments in Anaconda and something happened with the configuration of the python path VSCode uses for its integrated terminal: when I run my code, an ImportError comes up for the joblib package that I now have installed in my conda environment (also happens for other packages), which doesn't happen when I run the same code in a regular terminal.
Using conda list in the integrated terminal gives me the correct list of packages for the environment I'm using, and conda env list indicates that indeed this specific environment should be active. However, I can see that that something is off, because in my normal terminal I get:
$ which python
python is /Users/Joris/anaconda3/envs/astro3/bin/python
python is /usr/bin/python
While in the VSCode terminal:
$ which python
python is /usr/bin/python
python is /Users/Joris/anaconda3/envs/astro3/bin/python
I've tried changing some of the VSCode settings, like manually pointing python.pythonPath and python.venvPath to my anaconda environment folders. (The setting python.terminal.activateEnvironment is set to true.) Also, reinstalling VSCode through the Anaconda distribution does not seem to help. So unfortunately, I can't seem to get VSCode to run python with my Anaconda environment which contains the packages I need.
Press (macOS): ⇧+⌘+P, (Linux/Windows: Ctrl+Shift+P) in VS Code.
Type: python select interpreter
Select /Users/Joris/anaconda3/envs/astro3/bin/python
Test if your lib is usable.
I am quite new to python and pycharm.
I installed python 3.6 through anaconda, and I can see many packages including numpy are installed as I can see them in cmd (im using windows) by typing 'conda list'. Also, it works if i type 'import numpy' in python through window command prompt.
However, if I open pycharm and run "import numpy" there, it gives me 'No module named 'numpy' in pycharm'. May I know whats wrong with my setting? I guess it must be some problem with my interpreter setting.
I think my python is installed in C:\Users\AAA\Anaconda3\python.exe
I checked in pycharm, project interpreter is "C:\Users\AAA\PycharmProjects\untitled\venv\Scripts\python.exe"
Should I change it to the one under anaconda3 folder?
What is venv folder under "pycharmprejcts"? Is it a virtual environment? It shows (see the attached screenshot) the base interpreter is the one under anaconda? Should I choose to inherit global site-packages?
You should select Conda environment in Pycharm, not create a new, blank Virtualenv
Or at the very least - System interpreter, then find the Python executable for Anaconda
What is venv folder under "pycharmprejcts"? Is it a virtual environment?
Yes, it allows your project to be more portable - you define the minimum set of dependencies for your code rather than rely on everything installed only locally on your machine
You have a virtual environment, this helps keep the dependencies separate between projects. You can set your project interpreter to C:\Users\AAA\Anaconda3\python.exe or install the dependency in your venv, in pycharm you would press alt+enter on the import and press install.
I installed anaconda into my computer using python. After I install the software, I found there is one program called anaconda prompt.
What is the difference between anaconda prompt and command prompt? If I want to update the package, which one I should use or either one. Like (conda update conda)
Thank you
Anaconda command prompt is just like command prompt, but it makes sure that you are able to use anaconda and conda commands from the prompt, without having to change directories or your path.
When you start Anaconda command prompt, you'll notice that it adds/("prepends") a bunch of locations to your PATH. These locations contain commands and scripts that you can run. So as long as you're in the Anaconda command prompt, you know you can use these commands.
During the installation of Anaconda there is a choice to add these to the PATH by default, and if checked you can also use these commands on the regular command prompt. But the anaconda prompt will always work.
As far as updating conda, if it doesn't work in command prompt, you can do
conda update conda
in Anaconda command prompt.
When you use anaconda command prompt it opened at conda directory (path where all the conda commands run)
like when I was installing pip3 install prettytable on command prompt it successfully installed but not replicate in jupyter notebook.
But when install it using anaconda prompt it replicates intantly
Here is the difference:
PATH-TO behind is the path to Anaconda3
Afer adding the following env to Windows command prompt, it will be the same as Anaconda prompt:
set CONDA_DEFAULT_ENV=base
set CONDA_PREFIX=PATH-TO\Anaconda3
set CONDA_EXE=%CONDA_PREFIX%\Scripts\conda.exe
set CONDA_PROMPT_MODIFIER=(base)
set CONDA_PYTHON_EXE=%CONDA_PREFIX%\python.exe
set CONDA_SHLVL=1
set Path=%CONDA_PREFIX%;%CONDA_PREFIX%\Library\mingw-w64\bin;%CONDA_PREFIX%\Library\usr\bin;%CONDA_PREFIX%\Library\bin;%CONDA_PREFIX%\Scripts;%CONDA_PREFIX%\bin;%CONDA_PREFIX%\condabin;%Path%
I see great answers but maybe it is a little technical for beginners. Anaconda is a (software) distributor, in this case for Python, and its anaconda command prompt is similar to windows command prompt with one important difference: having PATH. PATH, as it can be assumed from its name, is like an address in computer where python packages are installed and their computations will be run.
Having PATH makes a big difference between using python via anaconda prompt or normal command prompt which is considered useful for many research teams and business computers: it will allow you to install, update and uninstall packages inside a separate environment (after creating one!) so that other members of the team wouldn't be disturbed.
When I was working with my own laptop, I never had any problem with running python on jupyter using windows command prompt. But for working with a research team PC, my supervisor made me to create a virtual environment named after me in anaconda prompt just not to disturb any other member. Actually, that was a pain in my neck because I have to activate "my environment" every time before starting python on jupyter. I never understand why someone should not appreciate if I install any more extra packages or updates for them? Not to mention, if in a million years an error pops up due to a package I installed, they can simply uninstall or downgrade it.
So as a normal researcher with medium computations in python, I never liked working with anaconda prompt and windows command prompt was everything I needed, especially on my OWN laptop.
I hope you liked my idea.