This is my current python environment setup on Windows 10:
For the remainder of this post I will assume you have chocolatey installed. Also I'm using the bash as admin terminal from cmder.
Install python3 with choco install python and/or choco install python3. Verify that you have the latest python3 version installed with python --version. Then type choco install pyenv-win. This is the easiest way to do this because it also configures the Environment Variables.
Now install the python version you want with pyenv. In this post I'll go with 3.9.0
pyenv update
pyenv install --list
pyenv install 3.9.0
It seems like no matter what you do, your path will always put /c/Python39/python aka not the pyenv version of python first. Even if you set the pyenv section of your path first by going to My Computer > Properties > Advanced System Settings > Environment Variables and move PYENV to the top of the path, your terminal will always see /c/Python39/python as the default version.
Then I install virtualenvwrapper with the pyenv version of python:
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:$PATH"
/c/Users/<myusername>/.pyenv/pyenv-win/shims/pip install virtualenv
/c/Users/<myusername>/.pyenv/pyenv-win/shims/pip install virtualenvwrapper
source /c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/virtualenvwrapper.sh
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:/c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/:$PATH"
I make a virtual environement like this mkvirtualenv test1. Now the terminal has a (test1) before the prompt so I know that the virtual env is active and working. Before you close out of this terminal, run the command deactivate to stop the virtualenv.
Now when I want to start a python project, I run these commands (Order is really important. If you do the first two commands out of order it won't work until you close and reopen the terminal).
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:/c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/:$PATH"
source /c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/virtualenvwrapper.sh
workon test1
And finished! That's how I setup my virtual environments in Python.
See how inconvenient that was lol. On my mac it's easy I just run pyenv virtualenvwrapper then workon test1. So how can I do this better? Is there a simpler way to work with python on Windows 10?
P.S. I would prefer not to use PyCharm. I know a lot of people like it but I'm looking for a more text editor/IDE agnostic solution.
Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:
I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:
The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):
I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:
I deleted and then reinstalled the Python interpreter from python.org.
I tried alias python='python3' (which I saw in one of the threads here).
I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here).
To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again.
I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error.
Anyone updating their macOS to Monterey 12.3 will find that they suddenly no longer have the system-provided Python 2.
The reason for this is that Apple removed the system-provided Python 2 installation (details).
So a workaround/solution for this is to use pyenv to install Python 2.7 (or any other specific version you need).
Install pyenv with brew to manage different Python versions: brew install pyenv
List all installable versions with pyenv install --list
Install Python 2.7.18 with pyenv install 2.7.18
List installed versions with pyenv versions
Set global python version with pyenv global 2.7.18
Add eval "$(pyenv init --path)" to ~/.zprofile (or ~/.bash_profile or ~/.zshrc, whichever you need)
Relaunch the shell and check that Python works, or run $ source ~/.zprofile (Thanks masoud soroush!)
OK, after a couple of days trying, this is what has worked for me:
I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much).
I installed python via brew rather than from the official website.
It would still return command not found error.
I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3.
Problem solved.
As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.
If you simply installed Python 3, just use python3 as the command instead of just python. In my case, I had to install pynev first via Homebrew (executable brew) using brew install pyenv.
But still after using pynev to install Python 2.7.18 and setting it as a global version using pyenv global 2.7.18, I still ran into an error while trying to run python.
What worked for me (since I already had Python 3 installed) was by changing my command to use python3 instead of just python. Of course, this won't be a solution to everyone who may want to use Python 2.
you should try
sudo ln -s /usr/bin/python3 /usr/local/bin/python
that solve my problem:)
I just had this problem on a new Macbook Pro with macOS Monterey, and the below worked for me using Homebrew.
Using alias is not necessary when using Pyenv. Tested with Atom 1.60.0 and atom-python-run 0.9.7.
Install pyenv (https://github.com/pyenv/pyenv#installation) and its dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment):
brew install pyenv
brew install openssl readline sqlite3 xz zlib tcl-tk
Install Python 3.10.6 but I assume other 3.x versions should work as well:
pyenv install 3.10.6
Add Pyenv to your shell according to the instructions in https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv. In your home directory:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Make the installed Python available everywhere (this can be overridden per project or folder, if necessary):
pyenv global 3.10.6
Now, the output looks like this:
% which python
/Users/jl/.pyenv/shims/python
% python --version
Python 3.10.6
If some scripts still fail, check that you have added Pyenv to the necessary shell startup file(s) as mentioned in step 3 above.
Since you installed python3.9> (https://python.org/downloads/) , you just need to run everything with python3 instead of python. You don't have to downgrade your python to use it... It works fine with suffixing python with 3, i.e. python3 eg.
$ which python3, python3 --version, $ python3 script.py etc
I installed Python then this error occurred and I just run
echo "alias python=/usr/bin/python3" >> ~/.zshrc
and I t worked.on macOS Monterey 12.4, Python 3.8.9
Try the command python3 --version if you have installed Python 3.
I had the same problem with fresh MacOS Ventura with Apple Silicon M1 Pro chip. Seems like brew installs to a different location than before on Apple Silicon?
/usr/bin/ folder had a Python executable and the version for it was 3.9.6.
So I installed Python using brew but then I had to execute it via python3 command so I ran echo "alias python=/opt/homebrew/bin/python3" >> ~/.zshrc and restarted my terminal. I was prompted with the desired version of Python when executing python command.
alias python=/usr/bin/python3
This command works but need to re-enter whenever we restart the termian
If you are using pyenv and on Mac M1, then try switching the python from system version to a version that is installed already (ex: pyenv global 3.8.13, considering 3.8.13 is another version). In order to check what versions installed use pyenv versions.
If you don't have another version, try installing a new one and switch to that new environment (ex: pyenv install 3.8.13). Then install using pip. Everything should work fine. thats what worked for me.
and make sure your pip and python are aliased to pip3 and python3 respective in the respective terminal configuration file.
I First installed python with brew
brew install python
Then I tried linking the python with symlinks using:
brew link python3
And it worked for me.
PS: I was trying to run a shell script and alias approach didn't work.
I got zsh: Command not found python after installing using https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg
I solved it by configuring the ENVIRONMENT. I added a line, alias python=/usr/local/bin/python3.10, into the .zshrc file (you must be sure it is the true path).
My macOS (Apple silicon):
Darwin Johns-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64
for 3.10.8
install with python with brew command
brew install python#3.10
if you have .zshrc file just edit like this
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
if you dont have .zshrc file:
cd
vi .zshrc
and copy/paste the line below
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
I have a MacBook Pro with an Apple M1 chip and macOS v12.4 (Monterey).
I deleted all Python versions and cleared the trash
Opened Terminal and typed "python"
This prompted an xcode.app update
Then I downloaded the latest version of Python from python.org (version 2.10.5)
Installed that version
Quit Terminal
Opened Terminal
Typed "python3"
It seems to now be there OK.
I use brew install brew install python#3.9, after installation success I want to know the version of python, I run command python --version, and I got the problem zsh: command not found: python.
If you install it successfully, please check in the terminal. It will show you if you got problem in install if not problem you just change command from python --version to python3 --version.
Just run the below command in your terminal:
echo "alias python=/usr/bin/python3" >> ~/.zshrc
Basically, here we are saying the terminal to treat python as python3.
Works like magic!
eval "$(pyenv init --path)"
bash starting will be more slow, just suggest
export PATH=$PATH:$HOME/.pyenv/shims
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".
As I understood, I have two versions of python 2.7 installed on my machine. One is located in /usr/bin and another one is in /usr/local/bin. When I type python in the shell, it calls one in /usr/local/bin, and it doesn't have access to all the packages installed using apt-get and pip. I have tried to set up an alias, but when I type sudo python it still calls one in /usr/local/bin. I want to always use one in /usr/bin, since I have all the packages there. How do I do that?
From what I understood,
You have two version of python. One is in /usr/local/bin/python
and another is in /usr/bin/python.
In your current configuration default python ->
/usr/local/bin/python
You want to use the one that is in /usr/bin.
Update your ~/.bashrc and append this line at the end
alias python=/usr/bin/python
Then open a new terminal. Or do source ~/.bashrc in the current terminal
Run which python to see the location of the python executable. It will show you /usr/bin/python
Also, if you want to get packages in your current python (i.e. /usr/local/bin/python) you can use pip with that particular python version.
Find pip location using which pip
Assuming pip location is /usr/local/bin/pip
/usr/local/bin/python /usr/local/bin/pip install
you can easily have two python version in your machine.
But first I recommend to install the Anaconda package.
And then you can create an environment with python 3 version
conda create --name test_env python=3 numpy pandas
In order to activate it, you need to write in your terminal
source activate test_env
More info here:
https://conda.io/docs/using/envs.html