How do Windows Paths and Anaconda virtual environments work together? - python

I started my Python journey a month ago by following a guide which recommended installing Anaconda. Since then I've written/adapted some Jupyter Notebooks using VS Code and run them from within virtual environments. Recently I needed to run one of these Notebooks via from MS Access VBA and got to understand a little about using Python from the command line - specifically about using Environment Variable Path functionality in Windows to access Anaconda\Scripts and use things like activate to active virtual environments.
In the video guide it also recommended adding Anaconda3 to my Path so I could access python.exe. However, I've held off doing this because even after plenty of reading I'm still not clear on exactly the mechanics of of using this command when working with Anaconda virtual environments.
So to my question... say I add the Anaconda3 folder to my Path and then write something like python myscript.py presumably it runs the python.exe interpreter from the Anaconda3 folder? How will this work if I'm in a virtual environment that needs Python 3.7 but the interpreter I've loaded is actually Python 3.6? Does this even matter?
Please be assured I've read for a couple of hours on this before posting my question. Happy to read more if there is a layperson guide someone can point me towards. I've read through the Anaconda site.

Related

Python not found

I'm working on a project in a python virtual environment, the project was started on a pc with python 3.8 installed and consequently python 3.8 was used in the virtual environment, or I had the need to continue the project on another pc, so I loaded all on GitHub, in the end I downloaded it to another PC with python 3.11 installed, the files are all there but when I try, inside the virtual environment, to open python by writing python or python3 in the terminal, it shows up this error: Python not found; Run with no arguments to install from the Microsoft Store or disable the link from Settings > Manage apps Run aliases. I then tried to see if it only showed up inside the virtual environment, but the same thing happens outside as well.
I tried to do various things among those suggested in other forums but they didn't work, the problem persists, I'm a bit lost, it's the first time this has happened to me.
Sorry to bother, maybe it's the simplest problem there is to solve but I don't know where to start
Trhanks
The problem is the different version of python on the two computers... the content of the pyvenv.cfg file must simply be changed which, when creating the virtual environment, is based on the installed version. Here you just need to change the path to reach the python.exe file installed on your computer and then change its version by inserting the correct one.

IDLE and PyCharm are using different versions of python, despite the python.exe PyCharm is using being installed in the same directory

I cloned a python environment and recreated it on a new machine using Anaconda Navigator with a yml file. My script runs fine in the new environment using PyCharm. The python version of this environment is 3.9.0. However the script doesn't run with IDLE because IDLE is running version 3.11.0.
IDLE is located at:
C:\ProgramData\Anaconda3\envs\CloneETL3\Lib\idlelib\idle.pat
The python interpreter which PyCharm uses is located at
C:\ProgramData\Anaconda3\envs\CloneETL3\python.exe
How can IDLE have a different version from the python interpreter when they were just installed fresh? How can I prevent multiple versions of python from running on my machine? I made sure to create a new project in PyCharm, use an existing interpreter using conda and point it to the exe I described above.
The environment was first created from a clone of my ArcGIS Pro environment.
Thanks for the help!
This is quite common in most editors. I use vscode which is similar to pycharm and the user can select the version of python (or other languages) that they wish to run.
It look like this:
The reason for this is that some users have the requirement of being compatible with previous versions. You should be able to select the latest version of each.
Alternatively you can delete all versions leaving only the one version that you desire, this would avoid confusion...
You could refer to this answer for that option: How to completely remove Python from a Windows machine?

How to use a Virtual Environment?

I am using Python 3.7.9 Shell.
I created a virtual environment in this location
C:\Users\my_username\Desktop\Projects.venv
Inside of Python Shell, when I type: import numpy, which is in my .venv\lib folder, it says that the module does not exist.
Using Python Shell, how do I make use of the contents in .venv? In particular, the libraries located there?
Edit #1: Include Details
In my windows command line, it has (.venv) off to the left.
I have run the Activate file. I then started Python.
In my \lib\site-packages area, I have the requests library.
When I open up Python Shell and type "import requests", it says "no such library can be found"
I am using Windows 10
I installed the libraries while in the (.venv) environment.
Theory:
In the virtual environment, in Python Shell, it's searching a different location for libraries...now if I can just figure out where it's searching and how to change that...I might be able to make progress.
Edit #2: My Progress
My theory was correct. Despite using a virtual environment, it's not looking for the libraries installed in (.venv)\lib\site-packages, it's looking somewhere outside of that.
Now I just need to figure out how to make the Python code look for libraries inside of (.venv)\lib\site-packages when I'm in the virtual environment.
When I run the python.exe file inside of the (.venv)\Scripts location, it recognizes the virtual environment scripts.
If I click on my version of Python.Exe located in my C:...\Programs\Python 3.7 folder, it doesn't recognize them.
I was under the impression it didn't matter where I clicked on the Python.exe file if I did it after going to the virtual environment in the command line? Is this not true?
Edit #3: Important Links
Where Python Looks for Modules When Importing
Right from the official docs https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
this is done in your shell before starting python at its prompt, and allows you to choose different python versions in addition to other benefits

How can I switch virtual environments with Spyder without going to preferences?

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

How to set default interpreter and keep things in order?

I was required to install anaconda for a CS course and used spyder and Rstudio.
Then, for a different class I used pycharm.
When I type on the command line "python -V" I get:
Python 3.6.1 :: Anaconda 4.4.0 (x86_64)
and I have no idea why it relates the python version I have installed with Anaconda (and why not pycharm?). I understand that the OS runs python 2.7 (shouldn't I get that instead? and when I type python3 -V get which version of python 3 I have?) and when I use something like Pycharm or Spyder I can choose which version I want from the ones I have installed and use it within the program, not for the terminal.
I just want to have everything in order and under control. I don't think I understand what Anaconda really is (to me is like a program that has more programs in it...). How do I keep anaconda to itself ? 1313
Also, should the packages I installed through Terminal work on both pycharm and spyder/anaconda even though when I used pycharm I used python 3.5 and anaconda 3.6?
I think I need definitions and help to get everything in order in my head and the computer.
Pycharm is just an application to help you write code. Pycharm itself does not run python code. This is why in PyCharm, you need to set the interpreter for a project, which could be any python binary. In PyCharm, go to Preferences > Project > Project Interpreter to see where you would set the python environment being used for a given project. This could point to any python installation on your machine, whether that is the python 2.7 located at /usr/bin/python or a virtual environment in your project dir.
The industry standard way to "keep things in order" is to use what are called virtual environments. See here: https://docs.python.org/3/library/venv.html. A virtual environment is literally just a copy of a python environment (binaries and everything) so whatever directory you specify. This allows you to configure your environment to however you need in your project without interfering with other projects you might have. For example, say project A requires django 1.9.2 but project b requires 1.5.3. By having a virtual environment for each project, dependencies won't conflict.
Since you have python3.6, I would recommend going to you project directory in a terminal window. Running python -m venv .venv to create a hidden directory which contains a local python environment of whatever your 3.6 python installation. You could then set your project interpret to use that environment. to connect to it on the command line, run source .venv/bin/activate from where you created your virtual environment. run which python again and see that python is now referencing your virtual environment :)
If you are using a mac (which I believe you are from what you said about python2.7), what likely happened is that your anaconda installer put the Python bin directory on your PATH environment variable. Type in which python to see what the python alias is referencing. You can undo this if you want by editing your ~/.bash_profile file if you really want.
You are more or less correct about anaconda. It is itself another distribution of python and contains a load of common libraries/dependencies that tend to make life easier. For a lot of data analysis, you likely won't even need to install another dependency with pip after downloading anaconda.
I suspect this won't be all too helpful at first as it is a lot to learn, but hopefully this points you in the right direction.

Categories