Add Python to the Windows path - python

If I forget to add the Python to the path while installing it, how can I add it to my Windows path?
Without adding it to the path I am unable to use it. Also if I want to put python 3 as default.

Edit Path in Environment Variables
Add Python's path to the end of the list (these are separated by ';').
For example:
C:\Users\AppData\Local\Programs\Python\Python36;
C:\Users\AppData\Local\Programs\Python\Python36\Scripts
and if you want to make it default
you have to edit the system environmental variables
edit the following from the Path
C:\Windows;C:\Windows\System32;C:\Python27
Now Python 3 would have been become the default python in your system
You can check it by python --version

Related

How do I set standard Interpreter path for python in vscode config json?

I use fish shell and I just want to when I open the vscode it automatically selects the virtual environment that was activated in virtual fish.
I want to have something like this:
"python.interpreterPath": "${VIRTUAL_ENV}/bin/python"
(this variable actually doesn't exists, I just put it as an example)
Try: Ctrl/Cmd+Shift+P: Python: Select Interpreter. It may suggest the correct path by itself, else you have to enter the path to the python executable you want to use by yourself.
Search in the fish documentation wether there is support for automatically passing the right interpreter path to VS-Code.
But that's the way how to do it manually at least.

which python vs PYTHONPATH

If I type in which python I get: /home/USER/anaconda3/bin/python
If I type in echo $PYTHONPATH I get: /home/USER/terrain_planning/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages
Should that not be the same? And is it not better to set it: usr/lib/python/
How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?
You're mixing 2 environment variables:
PATH where which looks up for executables when they're accessed by name only. This variable is a list (colon/semi-colon separated depending on the platform) of directories containing executables. Not python specific. which python just looks in this variable and prints the full path
PYTHONPATH is python-specific list of directories (colon/semi-colon separated like PATH) where python looks for packages that aren't installed directly in the python distribution. The name & format is very close to system/shell PATH variable on purpose, but it's not used by the operating system at all, just by python.
which python is the path to your python interpreter. PYTHONPATH is an environment variable where your Python program can search for modules to import.
See section 1.2
Should that not be the same? And is it not better to set it: usr/lib/python/ How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?
No they are not the same. You don't really need to modify the path to your Python interpreter. To modify the PYTHONPATH, you can set it in a shell, or from within a Python program by using sys.path
import sys
print(sys.path)
sys.path.append("another/path/to/search")

How to access my custom python scripts from CLI in order for them to act on any file?

I am having difficulty accessing my custom scripts from the command line (python3 unzipit.py "C:\Users\Me\downloads\zipfilehome"). I want to have my scripts act on any file, no matter where they are. I don't want to have the script file in the same directory in order for it to work. I followed this question's top answer to no avail. Note: I am using Windows 10 and all my python versions are in the path with no issues accessing them.
What I did (I always refresh my CLI with every change)
In the system environmental variables:
Path: (unchanged since installation) C:\Path2Python27;C:\Path2Python27\scripts;C:\Path2Python37;C:\Path2Python37\scripts;
PYTHONPATH: C:\Path2Python37;C:\Path2Python37\scripts;C:\Users\Me\myscripts\py
Things I also tried
system environmental variables
Path: C:\Path2Python27;C:\Path2Python27\scripts;C:\Path2Python37;C:\Path2Python37\scripts;C:\Users\Me\myscripts\py
moving the same stuff to user env's Path
moving the above-shown system PYTHONPATH to the user env
What else am I missing? I don't understand.
All in all, what I needed to get this working:
system environmental variables
Path: C:\Path2Python27;C:\Path2Python27\scripts;C:\Path2Python37;C:\Path2Python37\scripts;
PYTHONPATH: C:\Users\Me\myscripts\py
and
making sure to use Andriy's comment. It won't work using python3 unzipit.py "C:\link\to\folder".
In order to achieve what you want you have to specify -m flag and the module name, so python will retrieve module by looking up python module path. See more here in interface-options. The command shall be:
python3 -m unzipit "C:\Users\Me\downloads\zipfilehome"

Why would I add python to PATH

I am beginning to look at python, so when I found a tutorial it said that the first thing to do would be to download python from www.python.org/downloads/
Now when I downloaded python 3, I then started the installation and got to
Why would I want to "Add Python 3.5 to PATH"? What is PATH? Why is it not ticked by default?
PATH is an environment variable in Windows. It basically tells the commandline what folders to look in when attempting to find a file. If you didn't add Python to PATH then you would call it from the commandline like this:
C:/Python27/Python some_python_script.py
Whereas if you add it to PATH, you can do this:
python some_python_script.py
Which is shorter and neater. It works because the command line will look through all the PATH folders for python and find it in the folder that the Python installer has added there.
The reason it's unticked by default is partly because if you're installing multiple versions of Python, you probably want to be able to control which one your commandline will open by default, which is harder to do if both versions are being added to your PATH.
In addition to what #SuperBiasedMan stated, you can edit your PATH in Windows by hitting Start > Run, then type sysdm.cpl.
From there, navigate to Advanced tab and then hit Environment Variables.
In the lower section, where it says 'System variables', find the one named PATH and double click it. Note that it would be easier to copy and paste it to a notepad or something. The separator as you can see is a semi-colon.
Any path that you add to this variable, will be looked when you type any command in a cmd window or through the 'Run' command line.
That's the same concept as in Linux, I just pointed out how it can be edited.
This shows the way if you haven't add python to PATH(By the way, the python.exe is in my Python directory)
This is the way if you add python to a PATH

how do I add a python module on MacOS X?

I'm trying to use pywn, a python library for using WordNet. I've played about with python a little under Windows, but am completely new at MacOS X stuff. I'm running under MacOS 10.5.8, so my default Python interpreter is 2.5.1
The pywn instructions say: "Put each of the .py files somewhere in your python search path."
Where is the python search path defined under the default python installation in MacOS X?
If I've put the pywn files in /Users/nick/programming/pywn, what is the best way of adding this to the search path?
Is this the best place to put the files?
Try print sys.path from a Python shell. This will tell you what directories Python is searching for modules.
It is set via a combination of an environment variable (PYTHONPATH) and a base set of directories specific to your installation.
For more info: http://docs.python.org/library/sys.html#sys.path
More generally, if you do not have enough administrative rights to modify the system, or if you want to keep some modules in your home directory, you can either do:
import sys
sys.path.append('/Users/nick/programming/')
import pywn
You can alternatively add /Users/nick/programming to the environment variable PYTHONPATH, which has the advantage of giving you direct access to pywn through "import pywn". The default place on Mac OS X would be a .bashrc file in your home directory, which should then contain:
export PYTHONPATH="/Users/nick/programming/"
(separate multiple paths with ":", if necessary). You could then access pywn directly from any Python program with a simple "impot pywn".
I think by default /Library/Python/2.5/site-packages/ is part of your search path. This directory is usually used for third party libraries.
Simply use:
easy_install [LIBRARY NAME]
By the way... need to be root so before that:
su root

Categories