How to set default python version

How to set default python version

Change default python version for command prompt

On my machine I have python3.4 and a recently downloaded python3.6 installed, I would like to set python3.6 as my default python used in command prompt.

I have removed the python34 path variable but my when I access python through the command prompt it still defaults to opening python3.4. I saw this question about access the non default python but that was for specific files.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Since posting the question:

I have also removed python34 from the system path variables.

Typing where python in command prompt returns this:

Typing echo %PATH% returns

The accepted answer on the proposed duplicate states it is possible but you shouldn’t do it, and doesn’t give details on how to do it.

4 Answers 4

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

You can run multiple python versions on your machine.

To make Python 3.6 default, just put path of Python 3.6 before path of Python 3.4 in system variables.

In python34 directory on C drive rename python.exe to python3.4.exe. After doing this if you run command python in command prompt it will open python 3.6. And to run python 3.4 you need to run command python3.4 as we have renamed it to python3.4.exe in python34 directory.

After making changes in system variables in environment variables you just need to restart your command prompt. No need to restart machine.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

After some communication I guess I know what is the problem. Have you checked your system wide environment variables? Here is a link about how to:

Further, your application(your command prompt) wide environment variables are appended after system wide ones.

How to change default Python version?

How can I change the default Python version?

19 Answers 19

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

[updated for 2021]

(Regardless if you are on Mac, Linux, or Windows:)

If you are confused about how to start the latest version of python, on most platforms it is the case that python3 leaves your python2 installation intact (due to the above compatibility reasons); thus you can start python3 with the python3 command.

Historically.

The naming convention is that generally, most scripts will call python2 or python3 explicitly. This happened due to a need for backwards compatibility.

Even though technically python doesn’t even guarantee backwards compatibility between minor versions, Python3 really breaks backwards compatibility. At the time, programs invoking ‘ python ‘ were expecting python2 (which was the main version at the time). Extremely old systems may have programs and scripts which expect python =python2, and changing this would break those programs and scripts.

At the time this answer was written, OP should not have changed this due to maintaining compatibility for old scripts.

Circa year 2021.

Basically if you are writing a library, you should specify the version of python (2 or 3, or finer-grained under specific circumstances) you can use. Otherwise as an end user, you should feel free to rename this for your own personal use (though your OS or distribution may not make that easy).

Shell alias:

Confused users should not try to create aliases or virtual environments or similar that make python execute python3 ; this is poor form.This is acceptable nowadays, but PEP 394 suggests encouraging users to use a virtualenv instead.

How Should I Set Default Python Version In Windows?

What’s going on here?

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

17 Answers 17

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

This is if you have both the versions installed.

Go to This PCRight-clickClick on PropertiesAdvanced System Settings.

You will see a top half for the user variables and the bottom half for System variables.

Check the System Variables and double-click on the Path (to edit the Path).

Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.

Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

The Python installer installs Python Launcher for Windows. This program ( py.exe ) is associated with the Python file extensions and looks for a «shebang» comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.

Here’s how to check if the launcher is registered correctly from the console:

py.exe can also be run with switches to force a Python version:

How to set Python’s default version to 3.x on OS X? [duplicate]

I’m running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

21 Answers 21

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Changing the default python executable’s version system-wide could break some applications that depend on python2.

However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put alias python=’python3′ in your

/.profile in your

/.bash_profile and/or your

/.zsh_profile with a line like:

This way, your alias will work across shells.

If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:

Tip: For scripts, instead of using a shebang like:

This way, the system will use python3 for running python executables.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

/.bash_profile instead of

/.bash_profile after edit

You can solve it by symbolic link.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Then put the alias as follows:

Now save the file and then run the

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

/.bash_profile, alias pip=’pip3′, source

/.bash_profile everytime I re-opened my terminal? It reverts to python2 once I close and open the terminal again.

/.bash_profile instead of using. vi

I encountered this issue as well, so I thought I should post an updated answer. Please note that this will only apply to a Mac-based setup (I haven’t tried it with Windows or any flavor of Linux). The simplest way to get this working is to install Python via Brew. If you don’t have brew installed, you will need to do that first. Once installed, do the following in at the terminal:

This will install Python 3. After it’s installed, run this:

You will see all of the links created by brew to its Python install. It will look something like this:

The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:

You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:

You’re all set now. Now, you can do:

and it should show:

All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Go to terminal type:

This will setup default python as python3.x

This worked for me. I added alias and restarted my terminal:

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

The following worked for me

Go to ‘Applications’, enter ‘Python’ folder, there should be a bash script called ‘Update Shell Profile.command’ or similar. Run that script and it should do it.

Update: It looks like you should not update it: how to change default python version?

I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.

You have to add/modify your commands in

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Mac users just need to run the following code on terminal

3.x.x should be the new python version.

This will update all the system links.

UPDATE

For Newer version of MAC use

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:

This is the best way to manage multiple versions of python on a system in my opinion.

I think when you install python it puts export path statements into your

/.bash_profile file. So if you do not intend to use Python 2 anymore you can just remove that statement from there. Alias as stated above is also a great way to do it.

Here is how to remove the reference from

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

On MacOS

Setting PATH for Python 3.8

PATH=»/Library/Frameworks/Python.framework/Versions/3.8/bin:$» export PATH

Step-4: Save the file. And compile it by:

Step-5: Check the python version:

Step-6: Thats all.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

This is the simplest way from my exp. (if you have brew installed on your mac).

Try this from your terminal:

brew install python3

and then run the below on your terminal :

Tip:

** (note down the python version 3.8 or 3.9 thats displayed on the terminal. This will be required in the next step). for e.g. in my case it was:

Now run the below command on your terminal:

(where 3.9 is the version displayed on your terminal with the previous command)

To test your default version of python:

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

I’m not sure if this is available on OS X, but on linux I would make use of the module command. See here.

Set up the modulefile correctly, then add something like this to your rc file (e.g.

This will make it so that your paths get switched around as required when you log in without impacting any system defaults.

For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.

install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

If you are using macports, that has a easier way to do:

port install python37

after install, set default:

restart your cmd window, finished.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Well. It’s kinda old. But still deserves a good answer.

And the good one is You Don’t Wanna Touch The Default Python On Mac.

Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it’s still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide. whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.

The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency. hope you understand what I mean. A good developer doesn’t care. Good developers create clean playgrounds with python version they desire.

The right and wrong way to set Python 3 as default on a Mac

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

I’ve been dipping my toe back into Python development as I get ready to head to PyCon US. (If you’re headed there as well and want to share your Python story, let me know!) When I installed a module to tinker around with, I got a reminder that I needed to install Python 3 soon.

So, I did what any of us would do and googled around looking for a guide to update my development environment, which runs on Mac (the macOS operating system, formerly known as OS X). To my surprise, I found only a handful of StackOverflow posts, and they pointed me to partial solutions. Here’s the full story of how to set up your environment without breaking anything built into the macOS operating system.

1. Install pyenv

Moshe Zadka cautions that doing this wrong could result in an unreliable idea of which Python is running that depends too closely on shells loading aliases. I knew Moshe was familiar with Python, but what I didn’t know is that he is an author of many Python tutorials as well as an upcoming book on Python development on macOS. He helped 40 colleagues develop Python safely and consistently on macOS systems following one core principle:

«The basic premise of all Python development is to never use the system Python. You do not want the Mac OS X ‘default Python’ to be ‘python3.’ You want to never care about default Python.»

How do we stop caring about the default? Moshe recommends using pyenv to manage Python environments (for a deeper dive on configuring pyenv, see this article). This tool manages multiple versions of Python and is described as «simple, unobtrusive, and follows the Unix tradition of single-purpose tools that do one thing well.»

While other installation options are available, the easiest way to get started is with Homebrew:

2. Install Python

Now let’s install the latest Python version (3.7.3 as of this writing):

3. Set your global default

Now that Python 3 is installed through pyenv, we want to set it as our global default version for pyenv environments:

The power of pyenv comes from its control over our shell’s path. In order for it to work correctly, we need to add the following to our configuration file (.zshrc for me, possibly .bash_profile for you):

After that command, our dotfile (.zshrc for zsh or .bash_profile for Bash) should include these lines:

Now we know for certain that we’re using Python 3.7.3 and pip will update alongside it without any manual aliasing between versions. Using Moshe’s recommendation to use a version manager (pyenv) enables us to easily accept future upgrades without getting confused about which Python we are running at a given time.

Success

As you get comfortable with this workflow, you can use pyenv to manage multiple versions of Python. It’s also essential, for dependency management, to use virtual environments. I mention how to use the built in venv library in the article, and Moshe recommends virtualenvwrapper for managing virtual environments.

Understanding Python runtimes

Now that you have your Python versions fixed, it’s safe to explore why this problem confuses so many people.

The version of Python that ships with macOS is well out of date from what Python recommends using for development. Pondering Python runtimes can be comically challenging at times, as noted by XKCD.

How to set default python version. Смотреть фото How to set default python version. Смотреть картинку How to set default python version. Картинка про How to set default python version. Фото How to set default python version

Many users have dozens of Python interpreters on their computer already, but have no idea how to manage them effectively. Too often, people just download the latest Python release, move it to their path, and call it a day (or use brew install python3, which would do something similar). This can cause breakages down the line in frustrating ways that can be difficult to troubleshoot.

What NOT to do

My first idea on how to make Python 3 the default Python on my system was to move the old version and add the new one:

The pattern followed what /usr/bin/ usually does between major releases of Python, but I quickly learned it was the wrong move:

Thankfully, macOS protected me from breaking something I don’t fully understand. Further research proves this is exactly what we shouldn’t do.

Another thing not to try

Programming and development

Now that we know what not to do, let’s look at what we could do. There are a couple options when we think about common installation patterns for applications on macOS.

Use Python 3 as the macOS default

Python’s website has a macOS Python 3 installer we can download and use. If we use the package installation, a python3 fill will be available in /usr/local/bin/.

Aliasing is a must since the Python binary stored in /usr/bin/ can’t be changed. What’s nice about an alias is that it’s specific to our command-line shell. Since I use zsh by default, I put the following into the .zshrc file:

If you are using the default Bash shell, you can append this same text to your .bashrc:

This strategy works, but it isn’t ideal for making future updates to Python. It means we have to remember to check the website and download the new files since Python doesn’t include a command-line way to update.

Have Homebrew manage Python 3

The Homebrew project provides a free and open source package manager for macOS that many people rely on. It gives Apple users a power similar to apt-get or yum. If you are a Homebrew user, you may already have Python installed. To quickly check, run:

If Python shows up under the command, it’s installed. What version is it? Let’s check:

Okay, great! The Homebrew maintainers have updated the default Python bottle to point to the latest release. Since the Homebrew maintainers are more dependable at updating the release than most of us, we can use Homebrew’s version of Python 3 with the following command:

Now we want to point our alias (from above) to the copy of Python that Homebrew manages:

To make sure the path above points to where Homebrew installed Python in our environment, we can run brew info python and look for the path information.

This method, of using Homebrew to manage our Python environment, is a good starting place, and it made sense to me at the time.

What if we still need Python 2?

It makes sense for anyone new to Python to begin with Python 3. But those of us who still need Python 2—for example, to contribute to a Python project that’s only available in Python 2—can continue to use the default macOS Python binary available in /usr/bin/python:

Homebrew is so wonderful, it even offers a different formula for Python 2:

At any time, we can remove the aliases from our shell’s configuration file to go back to using the default copy of Python on the system.

Don’t forget to update pip to pip3!

The pip command is the default package manager specifically for Python packages. Although we changed our default Python command to be version 3, we have to alias our pip command separately if it’s on the previous version. First, we need to check what version we’re on:

To ensure we’re installing packages compatible with our new version of Python, we’ll use another alias to point to the compatible version of pip. Since we’re using Homebrew as our package manager in this situation, we know it installed pip3 when we installed Python 3. The default path should be the same as Python 3, but we can confirm this by asking the shell to find it:

Now that we know the location, we will add it to our shell configuration file, as we did before:

Last, we can confirm that running pip points to pip3 by opening a new shell or by resetting our current shell and seeing what we point to:

We can avoid using Homebrew to update pip, but that requires a much longer tutorial from the Python documentation.

Do it right from the start

If you are just getting started with Python development on a macOS, do the necessary configurations to make sure you’re using the right version of Python from the start. Installing Python 3, with or without Homebrew, and using alias will let you start coding, but it’s not a good strategy for the long run. Using pyenv as a simple version management solution to get you off to a good start.

This article was originally published in May 2019 and has been updated by the editor.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *