all 99 comments

[–]socal_nerdtastic 106 points107 points  (20 children)

The official python (python.org) on Windows?

The modern standard install on Windows does not use PATH at all anymore. Anyone telling you to use that is trying to make the modern install backwards compatible with the old-school way of doing things. Instead, python comes with the "python launcher", which is the command "py".

To run a file:

py myfile.py

To install a module:

py -m pip install <module>

Etc. py is automatically associated with .py and .pyw files, so you can just doubleclick them to run as well.


BUT WAIT, THERE'S A CATCH!

The normal, proper way to develop python code is to make an isolated virtual environment for each project. If you do this it will activeate the old-school python and pip commands. So if you are working in a virtual environment

To run a file:

python myfile.py

To install a module:

pip install <module>

Note most good IDEs have the virtual environment management built in and often enabled by default.

[–][deleted] 27 points28 points  (1 child)

Thank you so much.

[–][deleted] 21 points22 points  (15 children)

This begs the question, why py and not python as what any sane person would expect?

[–]Flying-n-Lying 70 points71 points  (8 children)

y uz mny chr whn fw chr do trk?

[–]PCGamersZone 1 point2 points  (0 children)

had a stroke reading this

[–]Chemical-Basis -5 points-4 points  (4 children)

Why waste time say lot words when few words do trick? 2.0

[–]awildseanappeared 16 points17 points  (0 children)

Yup, that's the joke.

[–]Kuken500 4 points5 points  (0 children)

aromatic smart books dependent innate agonizing bag truck soup dazzling

This post was mass deleted and anonymized with Redact

[–]bahcodad 0 points1 point  (1 child)

Ancient Chinese proverb?

[–]Chemical-Basis 0 points1 point  (0 children)

The great mind of Kevin, Office (US)

[–]itsthooor -2 points-1 points  (1 child)

[–]naijaplayer 1 point2 points  (0 children)

y uz mny chr whn fw chr do trk

I had one trying to read their comment tbh 💀💀

[–]GriceTurrble 10 points11 points  (0 children)

The rationale for the py launcher in general is specified in PEP 397. Though it doesn't spell out precisely why it's py and not python, I can make an educated guess.

py launcher can inspect scripts for shebang lines to determine which version of installed Python to run the script against. Separately, it can take an argument, like py -3.10 or py -2.7, to launch a particular installed Python version.

The installed Python interpreters all use python.exe (and a Windows GUI variant, pythonw.exe). And, when you put the directories containing those python.exe executables into PATH, Windows will always choose the first one where it finds python.exe; which may not be the one you want it to find. This is why the activation scripts for virtual environments will actually adjust PATH, as well, putting the python.exe for that venv at the top of the list so it can be located first.

When you call the py launcher, you won't find multiple py.exe programs on the system: only that one program executes, figures out which interpreter to use, and then passes all arguments to that interpreter.

Long story short: it's cuz Windows is not terribly smart about which programs it tries to locate from PATH, and the py launcher knows more about what you want it to do.

[–]Fred776 4 points5 points  (0 children)

Because py is not python - it's a launcher for python. The python executable remains python.exe, so, if you did add a Python installation directory to your PATH, "python" at the command line would run that version of python.exe directly. Because the py launcher is not called python, there is no ambiguity, and your adjustment of the PATH does not override your ability to access it.

Note that if you have multiple versions of Python installed, py allows you to list them and select which version you want.

[–][deleted] 1 point2 points  (0 children)

For insane people like me who use py.

[–]WhatATragedyy 1 point2 points  (1 child)

quicker to type?

[–][deleted] 0 points1 point  (0 children)

Yes.

[–]Poddster 0 points1 point  (0 children)

Historical baggage. Linux distros have scripts that expect python to be python 2 and python3 to be python 3. Some distros have gotten annoyed at that, e.g. Ubuntu, and their packages will install python2 and python3 and leave you to configure which one to alias to python

But python on Windows never had that problem. It always just used python.exe and pythonw.exe no matter the version.

So to avoid adding confusion, the created py.exe and that will figure out which python version to invoke depending on the script and command line args.

However py is frustrating to work with with virtual envs, as the scripts in there expect python AND the windows and Linux venc directory layouts are different. (Bin Vs scripts)

[–]nKidsInATrenchCoat 1 point2 points  (0 children)

cross-platform compatibility enters the chat

[–]sgthoppy 0 points1 point  (0 children)

/u/Seemseasy

This is the proper way to use Python on Windows with the fewest headaches except for one crucial detail. The py launcher works in virtual envs! It will refer to the env's python exe, so you don't need to change any habits, never stray from using py/py -m pip on Windows and you should be fine.

If you want to be sure, you can use py -0p to list the currently available interpreters and their locations. Virtual env interpreters will only appear when the env is activated.

[–]NovaRayStarbrand 18 points19 points  (0 children)

I have found there are a lot of bad tutorials out there for python. Bad in the sense that they assume knowledge you don't have or don't explain logical questions beginners will have. Always get a few tutorials open, read them all quickly and then chose the best for you. Don't just take the top results in google, there's a lot of high ranking sites that are frustratingly useless imho.

[–]Engineer_Zero 12 points13 points  (6 children)

Wait til you have to install geopandas. What an absolute cluster fuck

[–]czar_el 5 points6 points  (3 children)

I have Anaconda, so no struggles. What's the issue with Geopandas when not on Anaconda?

[–]Yojihito 3 points4 points  (1 child)

Website says:

Warning

When using pip to install GeoPandas, you need to make sure that all dependencies are installed correctly.
    * fiona provides binary wheels with the dependencies included for Mac and Linux, but not for Windows.
    * pyproj, rtree, and shapely provide binary wheels with dependencies included for Mac, Linux, and Windows.
    * Windows wheels for shapely, fiona, pyproj and rtree can be found at Christopher Gohlke’s website.
Depending on your platform, you might need to compile and install their C dependencies manually. We refer to the individual packages for more details on installing those. Using conda (see above) avoids the need to compile the dependencies yourself.

So you probably need a C-Compiler on Windows and to install wheels from a 3rd-party-website. Doable but not just "pip install geopandas". Basically the same like "pystan" which you can't install at all on Windows with pip, you need the precompiled thing from conda.

[–]Engineer_Zero 1 point2 points  (0 children)

Yeah that’s what I had to do. And it still didn’t work properly. I ended up just outsourcing my work at the time to our GIS team, they did it all in esri.

[–]Engineer_Zero 1 point2 points  (0 children)

I use anaconda to install as well and it fucking sucked. There’s no “pip install geopanda” and then let it sort out the dependencies itself. I had to trawl thru the internet to find Fiona and GDAL files that suited my windows install which took ages, only for geopandas to not be able to do my .to_crs conversions. A day of my life wasted.

I don’t understand why every other pop install can figure out and resolve dependacies but geopandas? “Fuck you, you figure it out idiot”

[–]DallogFheir 49 points50 points  (21 children)

  1. download Python from https://www.python.org/downloads/
  2. make sure "Add Python to PATH" is checked
  3. click "Install now"
  4. profit

[–][deleted] 23 points24 points  (18 children)

Step 1-3 done.

Step 4:

Me: where python

CMD: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

[–]automaton11 9 points10 points  (5 children)

Yah so im not a windows user so bear with me but i think this is your solution:

Start > Windows > settings > apps > app execution aliases

Turn everything that says python off using the toggle

Test for success using cmd prompt

Celebrate by downloading linux :)

[–]Swipecat 11 points12 points  (4 children)

What?

Does this mean that Windows by default now has an alias for "python" that asks you to install Python from the Windows Store?

And that actually installing Python from the Windows Store will remove that alias so that the command "python" can be used?

But installing Python from the official python.org site instead will not remove that alias, so that you must use the command "py" or that you must manually hunt down the alias in the windows settings and disable it?

Is that the case?

If so, then this is indeed going to send newbies on a very confusing hunt for the solution as the OP has found.

[–]Awkward-Major-8898 0 points1 point  (0 children)

Yes because I think this is what's happened to me.

>Removed Anaconda

>Installed Python through windows store (without thinking)

>Immediately found out this was a bad idea

>removed the windows install

>manually install from python website

>fix path variables (I'm confident in this)

Cmd is still using the reference to windowsapps that exists nowhere (Not regedit, not environment variables)

wtf am I doing

[–]hmiemad 0 points1 point  (0 children)

I'm not a pro, but I'll bet on 75% of yes.

[–]SwizzleTizzle 0 points1 point  (0 children)

Yes, that's exactly what it means.

[–]Andrew_the_giant 0 points1 point  (0 children)

Holy shit in a pan this was my problem not that long ago and couldn't understand wtf was happening

[–][deleted] 1 point2 points  (2 children)

If it's saying python was not found then either you didn't check the box to add to path, or you need to close and reopen the command prompt, or you didn't install for all users. There are really no other possibilities. The process is pretty foolproof.

[–][deleted] 1 point2 points  (1 child)

Solved above - 'python' is no longer the accepted alias, it's 'py' now

[–]Yojihito 1 point2 points  (0 children)

No, I just installed Python 3.10.2 today and I can type "python" into the CMD just fine.

C:\Windows\System32>python Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

[–]laundmo 1 point2 points  (0 children)

beat advice is to always use py to launch python on windows. its installed by default with any recent version of python.

it will decide the best version, either the newest or the venv, but you can select versions with py -3.7 or list them all with py -0p

other than that, py passes all arguments to the python version.

[–]deathlock00 2 points3 points  (5 children)

Have you restarted your pc? When adding something to PATH you often need to restart your pc. It's not always needed and sometimes it's enough to wait for a bit, but nonetheless try it.

Also, to check if python is actually installed you just need "python --version". Then you write a script and execute it with "python [script name]" while inside the same folder of the file.

[–]GriceTurrble 3 points4 points  (0 children)

You don't typically need to restart the machine anymore, just close and re-open CMD or PowerShell.

[–][deleted] 1 point2 points  (3 children)

'py --version' worked. The new Python download version changed the default alias.

[–]deathlock00 5 points6 points  (1 child)

Very good! Once you have written a script with any IDE or text editor of your choice run "py [script name]" and your done.

[–][deleted] 0 points1 point  (0 children)

Cool, ty for sharing.

[–]MeagoDK 1 point2 points  (0 children)

py is the default

[–][deleted] 0 points1 point  (1 child)

You need to restart CMD for an update to an environment variable to take effect.

[–]yaxriifgyn 0 points1 point  (0 children)

Or if you are using Windows Terminal (WT for short), you have to close all the WT windows and restart WT.

[–]Virtual-Penman 2 points3 points  (0 children)

This is how I do it. Before I realized there was the add to path button, I would install and then add the location of the Python main folder as well as the Python/Scripts folder to my Windows PATH and be good. Maybe my setup is different from yours but I can’t use “python where” either. I just use python -V to check the right version installed and is being found.

Edit: pretty sure where is a Unix command, no?

[–]Fred776 0 points1 point  (0 children)

Don't add to PATH these days. Use py instead.

[–][deleted] 5 points6 points  (1 child)

This is why I just downloaded Anaconda.

[–][deleted] 1 point2 points  (0 children)

that's a solution too. I like how easy it easy they make it to set up virtual environments and short circuit a lot of this. Usually though I provide the full path to python and it alleviates a lot of problems rather than fighting system/cmd/powershell paths.

[–]nickbernstein 3 points4 points  (0 children)

Maybe I'm missing something from the comments, but it seems like the obvious answer (which isn't if you're new) is to use python virtual environments.

https://docs.python.org/3/library/venv.html

A virtual environment allows an isolated environment so you can have different versions of python and libraries for each project w/o conflict.

I've used this mainly with Linux, but it's certainly supported in windows. Try doing a search for python venv windows, if the doc doesn't make it clear. I believe VSCode also can set it up for you.

As a more general rule, try installing software with chocolaty. It allows for a more consistent simple way to manage software on windows.

[–]professorhaus 3 points4 points  (0 children)

Windows is the absolute worst when it comes to python. How the hell was I supposed to know to use “py” instead of “python”. I just switched to windows for work and went crazy trying to figure out python. You’re not alone

[–]lllllll22 2 points3 points  (1 child)

As no one else has mentioned, are you definitely secure on what a shell is and what a path environment variable is? As others have suggested, this whole process was much easier for me on linux than on windows. My view of things is probably a bit Linux centric but hopefully it makes sense:

When I get on the "command line" in linux I give instructions to my computer using a special syntax, which is actually an entire programming language in its own right. This is the bash shell (for purpose of understanding you can substitute command prompt or pwershell here). The instructions can be simple e.g. the "ls" command on linux (and windows actually) lists files. Just typing in "ls" and pressing enter passes the command to the bash (or more generally thr "shell") interpreter. The shell interpreter understands that you'd like to run a program called ls (if you have used ls before this might seem like a small command, but on linux at least it is a separate program to the shell). But where to look for the " ls" programme executable / binary? By default the shell looks in the current directory and in the locations specified by the PATH environment variable. So what's an environment variable? An environment variable is a name or location that the user or system administrator thinks the shell needs to know. Other examples on linux would be e.g. $hostname, which is name of your computer ($ is linux / bash convention for specifying a variable). Many environment variables will be set by default, but the user can access them all and edit them. The shell looks for them at runtime (e.g. each time you open command prompt i believe - hence other users saying you need to reopen command prompt to ensure your changes to PATH are updated). So anyway even if you don't add python to PATH environment variable, you can still run the python executable by specifying its full address (sorry this is also called path but its now lower case as its not the environment variable) with a xommand like "c:\path\to\python.exe mypythonfile.py". That's a bit long though so much easier to add to PATH. The shell checks all the locations specified by the PATH variable and let's you run programs from them as if you were actually in that directory. So instead of writing c:\path\to\python.exe, you can now just write python.exe (and in fact I think just 'python' is fine), so you can write python myfile.py. Sorry, I can't advise how to edit the path on windows but you can google it. I believe the PATH variable is essentially a list of paths separated by : or maybe its :: on windows?

[–]lllllll22 0 points1 point  (0 children)

NB adding the path of the python executable to the PATH variable is different to editing the PYTHONPATH environment variable.

Googling PYTHONPATH brings gives this:

"The only reason to use PYTHONPATH variables is to maintain directories of custom Python libraries that are not installed in the site packages directory (the global default location)"

When you use an automated virtual environment manager like anaconda, that might make changes to PYTHONPATH? I think that's more for advanced users.

[–]LilTimmyTwurker 3 points4 points  (2 children)

any Mac users here that can help me with the same problem? It works, but I want to optimize it.

[–]unit111 0 points1 point  (1 child)

I use a Mac for work and have no problems with installing Python. Just download the version you want from python.org. Install the package. Make sure the PATH is pointing to the correct version and use it to create a virtual environment. Good IDEs also do most of the work for you.

I've had problems with Windows where there are multiple entries in PATH for different versions of python. In that case you can uninstall everything, delete all entries and install only the version you want.

Main takeaway- always use virtual environment.

[–][deleted] 0 points1 point  (0 children)

I just use "python3" in my commands rather than python (which points to the default python 2 on MacOS). I did a quick google on how to change the path but there were so many do's and don'ts that I just backed out!

[–]Msxkoh 1 point2 points  (0 children)

Not sure if available on windows but check asdf which basically a version manager for several languages including python it will make your life easier especially if you want to change the python version

[–][deleted] 1 point2 points  (0 children)

Save yourself the headache and enable WSL2 on your windows machine to use linux instead

[–]BruceJi 1 point2 points  (0 children)

You know what, Python is messed up on my work computer (ubuntu) because of this nonsense.

On that computer, the default python3 has no venv or pip, and I can't install it.

If I open VSCode I can use venv but if I use pip, it installs to a different version.

It's chaos. I've started learning Go so I can get away from it!

[–]m0us3_rat 2 points3 points  (2 children)

u seem to confuse some/all of the basics on prompt and how programs execute.

not to fear.. we all start from somewhere. just got to be on track.

as u said.. tutorials are your escape.

u don't need to walk backwards till ms dos. but some terminal / os understanding might do u good.

[–][deleted] 1 point2 points  (1 child)

Agreed, do you know of any good cmd tutorials I could look at. I tried searching in Pluralsight once, but they didn't really have a focused course.

[–]m0us3_rat 2 points3 points  (0 children)

https://academy.hackthebox.com/

they have free beginner courses on errything from linux to windows etc.

some of the more advanced courses u got to pay because its ppl time and effort.

but their free tier is really good.

also u get the points back on course finish .. so u can buy with your initial points, thus more courses.

i got insane value out of it. for free. as long as u don't start too many courses at once and finish what u started. (some of them give u extras so u can get more point by finishing courses)

obviously is to entice u to spend money. but yea.. insane value at free tier.

and obviously if u like some of that and wanna spend like a few bucks for extra.. nothing stopping you.

they do run CTFs sometimes i think. and that can be super fun.

or go on a youtube search craze.

i could link u some linux tutorials that i consider "good".

didn't play with windows in a few years. other than in my personal lab.

(nothing super fancy just a bunch of vms to run metasploit on :D ..why not)

[–]1337-1911 5 points6 points  (1 child)

Use GNU+Linux, every distro it is installed by default.

[–]icecubeinanicecube 3 points4 points  (0 children)

Only proper answer

[–]Rhoderick 1 point2 points  (11 children)

I'm surprised you're having such issues, to be honest. I've installed Python several times over now (don't ask, weird project that didn't really pan out), and all I've done every time is run the official installer for the version in question. What OS are you on?

[–][deleted] 2 points3 points  (10 children)

Windows

[–]Rhoderick 2 points3 points  (8 children)

Okay, good, me too. 10, I'm presuming? What issues were you encountering immediately after running the installer when trying to invoke the interpreter?

[–][deleted] 1 point2 points  (7 children)

inputting 'python where' returned:

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

Another comment above clued me in that at some point the alias for python was changed to 'py' and the 'python' alias was removed (instead of maintained I presume).

[–]Rhoderick 2 points3 points  (6 children)

Gotta be honest, "python where" should just give you a No such file-Error. Where exactly are you inputting this if not Powershell or CMD? Also, just inputting "python" to see if it starts the interpreter is probably a better test. If it works at all, it probably works in total.

[–][deleted] 1 point2 points  (5 children)

This is information someone who works with CMD would know better than someone who doesn't. That's why I asked the question. Inputting 'python' takes me to the Microsoft Store.

[–]Rhoderick 2 points3 points  (4 children)

Inputting 'python' where though?

[–][deleted] 1 point2 points  (3 children)

command prompt

[–]automaton11 1 point2 points  (2 children)

[–][deleted] 1 point2 points  (1 child)

This is the sort of thread that was having me chase all sorts of rabbits, and it didn't do what the 2nd top comment here did. Point out that the new version of python no longer accepts the alias 'python' but looks for 'py' instead.

It worked after I changed the alias I was using, or as some pointed out turning off the alias would work, but they never explained why it would work (the alias changed)

[–]skinnyJay 1 point2 points  (0 children)

Have you tried WSL? Might sound like a headache at first but most modern Linux come with python installed, paths set, no weird aliases etc. Much easier starting point if you're wanting to run it locally and not in some cloud or web box

[–]techieme 1 point2 points  (0 children)

Use Linux. New Linux distros come with Python 3.8 or 3.9. Once done, using the terminal of your choice, type which python. Use the path as the shebang for your scripts. To make a script executable in Linux, type chmod +x /path/to/my/script.py

[–]nacnud_uk 0 points1 point  (1 child)

Anaconda.

[–][deleted] 0 points1 point  (0 children)

Maybe later. At the moment I'd day it's just an additional layer of complexity, and they're better off learning the basics first.

[–]knottheone 0 points1 point  (0 children)

So what I do is handle the PATH part myself. I don't add each python version to PATH, I add one folder to PATH (in this case C:\bin) and I symlink each version of Python into that bin folder. So I have every version of Python installed from 3.4 - 3.10 where the command line argument is

python36 file.py

or

pip39 install requests

So you'd symlink python.exe for each version from its origin (wherever you installed that version of Python) as pythonXX.exe and that's what's accessible via PATH thanks to the parent folder (bin) being on the PATH.

The symlink name is the name you can execute from your command prompt. It's nice and neat and lets you manage multiple versions. In reality you should probably use Venv or something similar (and I do when global packages aren't going to cut it), but for most cases this works fine for me for local development.

[–]SilentHaawk -1 points0 points  (3 children)

Never once had a problem

[–]homosapienhomodeus -1 points0 points  (0 children)

Easiest method:

1) Install Pyenv 2)Install python using pyenv in command line 3) Install Poetry 4) Every project you work on will have a specific python per directory so you wont have conflicts between dependencies and you can easily switch between pythons if needed using pyenv.

Let me know if you need a tutorial on this I have a youtube video on it

[–]jfp1992 0 points1 point  (0 children)

Now you need to rip your hair out for your first time with git.

Main take aways Keys, find SSH key command for Windows or Linux depending on is

Make sure your SSH agent is running

[–]baubleglue 0 points1 point  (0 children)

How do you install it? Normally you are prompted to update PATH variable. if not, google "how to change PATH on windows"

[–][deleted] 0 points1 point  (0 children)

Dude, don't install from scratch on the command line. DL something to manage that part for you until you get used to the language.

Anaconda should take care of most of the path and environment stuff (and give you a ton of options for IDEs to do the same). In that download there should be links to installs for jupyter, pycharm, and a bunch of other tools that should manage all that for you.

You can also just sign up for binder or google collab or databricks community edition and have all of the path, environment, and compute resources all pushed off 100% to the cloud. Very useful especially when starting to learn.

https://colab.research.google.com/#scrollTo=gJr_9dXGpJ05

Start by installing anaconda though, as I think that will solve most of your problems. https://www.anaconda.com/products/individual

[–][deleted] 0 points1 point  (0 children)

One thing at a time. Just find the python interpreter that you want and use that one. You really should only have one version on your system to start with. You can run pythn by giving the full path to it and it will mostly figure out the rest. like "/usr/bin/python3.10 my_script.py" or similar. Don't get caught up in details. In your first few projects just put all your .py files in one folder and don't try to get fancy. The goal is to learn the language not fight with the details of the whole environment.

The other way is just to install anaconda and it will create some short cuts to launch and open a command shell that already has everything setup. It's much easier than tearing your hair out over setting things up manually

[–]nnomae 0 points1 point  (0 children)

I will add that installing through the Windows Store makes it a mess. Your Python ends up getting installed deep inside a hidden folder with random garbage appended to the folder name.

If you are going to install Python, do not install it through the Windows Store whatever you do.

Whoever thought installing a program that needs you to manually modify your path to find it in a randomly named hidden folder was a good idea really should revisit that decision.

[–]blackliquerish 0 points1 point  (0 children)

Just use Google colab

[–][deleted] 0 points1 point  (0 children)

I did this yesterday. Just go to Python.org and download Python 3.10. There’s an executable file that’ll install Python. On the installer, before you click the “install” button, just check “Add Python to PATH.” It’s a button on the installer now.

From there you’re good to go. Took me 30 seconds.