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.
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.
I have been programming in Python using Spyder in combination with virtual environments for a while now. Before I started using virtual envs, I installed Spyder using pip. I am not very familiar with paths and environments, but this seemed the way to go when I repetitively ran into package compatibility issues. However, it has been bugging me that every time I want to switch environment, I need to change the working directory to the folder of the environment and I need to change the Python interpreter manually within Spyder.
When I was looking for a solution, I found that it is possible to install Spyder within a virtual environment. I did this and found out that opening Spyder from a virtual environment has the exact same working directory and interpreter as the standalone version (the one I started with). This means that I still have to change the interpreter and working directory manually... Am I doing something wrong or is the only way to switch environments by changing it in the preferences tab within Spyder?
Background info: Using Windows, Spyder 4.2.1, venv as described on https://docs.python.org/3/tutorial/venv.html
So here is my story:
As we know macos comes with pyhton 2.7 preinstalled. Years ago, I installed python 3.7.0 and all going good. I started working with Tensorflow and during that time tensorflow wasn't compatible with 3.7.0 so switch the version to 3.6.6 somehow :). After some time i installed anaconda and it installed 3.7.3.
At this point:
When i open my python IDLE shell, it runs on 3.6.6
anaconda Jupiter notebook shows 3.7.3.
Then i learned to create virtual environments through terminal and i use following code to create virtual environment:
pip install virtualenv
virtualenv project_env
And after i activate the virtual environment the python version it shows is 3.7.3.
Then recently i changed the terminal shell to zsh and now everything is kind of messed up. I'm
Terminal can't find conda commands. How to fix that?
Now i can't create virtual environment through virtualenv project_env. It says command not found. Why? It use to create virtual environments with python 3.7.3.
Now to create virtual environment i use python3 -m venv project_env this creates the virtual environment with python 3.6.6. If i use python3.7 -m venv project_env it creates environment with python 3.7.0.
I can't find a way to create virtual environments with python 3.7.3.
I need help with above troubles caused after installing zsh and help with accessing python versions wherever i need. How do i update python version 3.7.0 to latest or any specific version like 3.7.3. Hows python 3.8.4 to use, i mean like i had trouble importing tensorflow in python 3.7.0.
I hope you will understand my trouble and searched a lot for solutions. I'm so confused right now, couldn't even figure out the title of my problem.
Based on the fact your shell can't find conda now, you should look at your "path" environment variable. Whenever you change from one shell to another, you need to look at and possible create or modify the files the shell uses upon startup. These files setup your environment (PATH, PYTHONPATH, etc), create aliases, etc. You may need to create the .zshrc, .zprofile, .zlogin etc files and add the same settings as your previous shell's startup files.
Example:
If you used the bash shell previously you would need to modify the .zshrc file to include your settings in the .bashrc file. Syntax may be different, so a straight copy may or may not work.
Try this as a quick temporary fix (may or may not work, depending on your current ~/.profile settings:
Create a ~/.zprofile file with this code inside:
emulate sh
. ~/.profile
emulate zsh
references:
https://superuser.com/questions/187639/zsh-not-hitting-profile
http://zsh.sourceforge.net/Intro/intro_3.html
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 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.