I just entered a class as an introduction to Python, and so I downloaded GitBash and Andaconda on my Windows laptop. I navigated to my python file on GitBash and tried to execute my python file, to no avail. I searched this website for answers on what to do and tried as many solutions as I could find, but I had no luck. I am quite a noob at this, so I think I am missing something obvious. Perhaps just one line of code that could save me?
...
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ ls
python_intro.py
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ PATH=$PATH:/c/Python27/
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ export PATH="$PATH:/c/Python27"
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python python_intro.py
bash: python: command not found
TECH-TESTER+usd#Tech-tester MINGW64 ~/Desktop/Python-Essentials/PythonIntro
$ python
bash: python: command not found
Sorry if this is such a rookie question, it just seems that everything I find on the internet is above my level to understand, whereas I am at this very basic level.
Meanwhile, I thought Anaconda came with the same py launcher as "official" Python, and offered the same option to install it to somewhere on your PATH.
Try first to check that in a regular CMD.
where python
That will check if python is in the PATH or not.
From there, you can start using python, in a CMD or a git bash session.
Here is the process you can follow to run the Python on your Git Gash.
Launch the program Git Bash in the usual way that you launch Windows programs or VScode. A shortcut for Git Bash was created during installation.
At the command prompt, paste this command export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32". That will tell Windows where to find Python. (This assumes that you installed it in export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32", as we told you to above.)
Check to make sure that this worked correctly by entering the command python --version. It should say Python 2.7.8 (or 2.7.something), as shown in the figure below.
Assuming that worked correctly, you will want to set up git bash so that it always knows where to find python. To do that, enter the following command:
echo 'export PATH="$PATH:/C/Users/ComputerName/AppData/Local/Programs/Python/Python37-32"' > .bashrc. That will save the command into a file called .bashrc. It will be executed every time git bash launches, so you won’t have to manually tell the shell where to find python again.
Check to make sure that worked by typing exit, relaunching git bash, and then typing python --version again.
Apply above process inside your project folder so that you can use it on VScode.
Related
Suddenly my development server python does not work.
Error message is as follows:
root#scu4:~# python
-bash: /root/anaconda3/bin/python: Invalid argument
/root/anaconda3/bin/ is python installed directory.
How can I fix it?
this error is because its not opening python from directory in which its installed rather its opening from /root/anaconda3/bin/python and it requires some arguements.
you can try python -h or python --help
there can be a chance that after installing anaconda it might have upgraded to python 3.x version try
root#scu4:~#python3
might solve your problem.
If this doesnt help, sudo root and then run the following command : ls -lah /usr/bin | grep python it should produce some output if not python is relocated to someother directory.
If Anaconda is on docker stop the docker and start it again.
docker stop <container>
docker start <container>
docker attach <continer>
See this issue for details.
conda update -n base -c defaults conda
helped me.
I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.
What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".
I just installed python 3.6.2.
I then opened the mac terminal and typed:
python3 -V
and it said: "command not found"
I then changed the directory to the desktop and repeated the same command and it gave the same result. Then i tried:
type -a python
and it said: python: not found
You should firstly add the python executable path to the environment variable $PATH, then you'll be able to run the command wherever you are.
In your terminal:
touch ~/.bash_profile; open ~/.bash_profile
export PATH="path-to-your-python-folder:$PATH"
save it and exit (Command + Q)
force execute it with: source ~/.bash_profile
Reboot your system, then you should be able to run it.
If I do it in CMD, it works without issues, but if I try it in Git Bash it doesn't work. I like to use Git Bash as my only console, but I can't do that if it doesn't work with Python 3.4.
Example is in the picture below. This can be easily reproduced. Uninstall Python and Git if they are installed, install Python 3.4, install Git 2.5.1 and you get that result.
How can I make the Python interpreter work in Git Bash ?
The MinTTY terminal that is the new default terminal for Git simply doesn't support Windows console programs. I don't know why the decision was made to change the default terminal, but I know a few ways to work around this:
Write a Bash alias to launch python with winpty
Bash Alias (put in your .bashrc):
alias python=winpty py.exe
Note: As of Git for Windows 2.7.1, Winpty is included out of the box. winpty can be found installed at Git\usr\bin.
Write a Bash alias to launch python in interactive mode if there are no arguments:
Bash Alias (put in your .bashrc):
function maybe_py() {
if [ $# -eq 0 ]; then
/c/Windows/py.exe -i
else
/c/Windows/py.exe $#
fi
}
alias python=maybe_py
Launch python in interactive mode explicitly
Note that this may not work correctly using arrow keys to browse command history:
py -i
Or for scripts:
py script.py
What Is py.exe?
In case you are wondering why I'm referencing C:\Windows\py.exe instead of a particular python.exe installation, I wanted to explain a few benefits of using it (the Python Launcher for Windows:
It's installed with newer installations of Python (Python 3.3+)
It understands and attempts to use the specified installation of python in shebang lines
It works with Virtual Environments (shebang line example for venv)
For changing your preferred/system installation (e.g. for interactive mode), see this answer.
You need to explicit python interactive mode: python -i
You can define an alias in your .bashrc: alias python='python -i', but doing this, you will not be able to run a script file (i.e.: python script.py).
Found here:
Using Windows Python from Cygwin
Building onto #Darthfett's answer. I had to make sure there were quote marks and not reference the .exe files
So in the end in your .bashrc
alias python='winpty python'
alias pip='winpty pip' # Rescue pip as well
Then is all works
Python
Tawanda#Tawanda-PC MINGW64 ~
$ alias python='winpty python'
Tawanda#Tawanda-PC MINGW64 ~
$ python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Pip
Tawanda#Tawanda-PC MINGW64 ~
$ alias pip='winpty pip'
Tawanda#Tawanda-PC MINGW64 ~
$ pip -v
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
It's trying to open the console for the output. Unless you compile python or get a version for mingw you may need something like:
WinPty
Thanks for #darthfett 's answer, which largely solves the problem!
Just FYI: Same symptom also exists when your script is using import getpass; getpass.getpass(), and in this case python -i your_script.py will NOT fix it, but winpty python your_script.py still works like a charm. (Lucky that they at least provide Winpty out of box with recent versions of Git For Windows.)
So, to setup once (per virtual environment) and forget it, you can append this line at the end of your env/Script/activate:
alias python='winpty python.exe'
It will work in that bash console. (However, if you happen to be a vim user, it still won't work inside a vim when you do :python my_script.py in vim.)
You can configure the git bash console by editing the file in your "$HOME/.bashrc"
Add this line to your $HOME/.bashrc
export PATH=$PATH;c:/python34
When installing git for windows, choose to use windows default console window as shown in the picture below.
This option allows you to use interactive python or nodejs. Also getpass works on this console.
enter image description here
When installing git for windows, choose to use windows default console window as shown in the picture.
This option allows you to use interactive python or nodejs. Also getpass works on this console.