- How to install and switch between different python versions in ubuntu 16.04.
- How to install different versions of python?
- python 2.7:
- python 3.5:
- python 3.6:
- python 3.7:
- python 3.8:
- How to switch between different python version?
- Creating alias in bashrc:
- Using update-alternatives:
- Изменить версию Python3 по умолчанию в Ubuntu
- Configure a Python interpreter
- Python interpreters in PyCharm
- Setting an existing Python interpreter
- Change the Python interpreter using the Python Interpreter selector
- Change the Python interpreter in the project settings
- Modify a Python interpreter
- Creating a new Python interpreter
- Setting the default interpreter
- Managing interpreter packages
How to install and switch between different python versions in ubuntu 16.04.
When I work with python, sometimes I face some issues with python versions. Though I know which command will work on that case but during that parti- cular time I just forgot the exact commands and I have to search on google or stackoverflow for the exact commands.
Here I will keep the notes of installing python of different versions and chan-ging the version on per use.
How to install different versions of python?
python 2.7:
Run the following command:
Now if you run the python -V command, you will get the 2.7.12 as output. Normally installation path will be /usr/bin/python.
python 3.5:
Run the following command:
Now if you run the python -V command, you will get the 3.5.2 as output. Normally installation path will be /usr/bin/python3.5.
python 3.6:
Run the following commands:
Now if you run the python -V command, you will get the 3.6.10 as output. Normally installation path will be /usr/bin/python3.6.
python 3.7:
Run the following commands:
Now if you run the python -V command, you will get the 3.7.6 as output. Normally installation path will be /usr/bin/python3.7.
python 3.8:
Run the following commands:
Now if you run the python -V command, you will get the 3.8.1 as output. Normally installation path will be /usr/bin/python3.8.
How to switch between different python version?
If we have multiple python versions installed in our system, then python will use only one executable. Normally if we run a program called program.py by ‘python program.py’, then this program will be run by python2.7. If we check python version by ‘python -V’, it will return 2.7.12. But if we want 3.5 as the result of ‘python -V’ and the program will be run by python 3.5 executable, then we can change the default python executable in two ways.
Creating alias in bashrc:
We can create an alias within user’s home directory and add it to the bashrc. But it will only work for the current user.
Open the bashrc file by using the following command:
Add the following line to the below of the bashrc file .
Now if we use check python version using ‘python -V’, it will return 3.5.2. And if we run the program.py file using ‘python program.py’, then it will be run by python3.5 executable.
Using update-alternatives:
To switch between python version over the all users, we can use update-alternatives command.
We will set priority of each version using update-alternatives. Python executable with the highest priority will be used as default python version.
We will set the priority by the following commands:
Here I set the priority of python 2.7, 3.5, 3.6, 3.7, 3.8 as 1, 2, 3, 4, 5. As the python3.8 has the highest priority, the default python executable is python3.8 now.
To switch between any versions, we can use the following command:
It will give a response as following:
Now default python executable can be changed by setting its corresponding selection number. For example, if we enter 4 , python will use python3.7 as default executable.
This story is just for my documentation purpose. Nothing serious 😀 😀
Источник
Изменить версию Python3 по умолчанию в Ubuntu
Я использую Ubuntu 16.04 LTS. Я python3 установил. Установлены две версии, python 3.4.3 и python 3.6 . Всякий раз, когда я использую python3 команду, она принимает python 3.4.3 по умолчанию. Я хочу использовать python 3.6 с python3 .
python3 —version шоу version 3.4.3
Я устанавливаю, ansible который поддерживает version > 3.5 . Поэтому, когда я набираю ansible в терминале, он выдает ошибку из-за python 3.4
Покажет вам ошибку:
Вам нужно обновить свою версию update-alternatives , тогда вы сможете установить версию Python по умолчанию.
Установите python3.6 по умолчанию.
Или используйте следующую команду, чтобы установить python3.6 по умолчанию:
Вы можете добиться этого, применяя следующие простые шаги —
- Проверьте версию Python на терминале — python —version
- Получите права пользователя root. По типу терминала — sudo su
- Запишите пароль пользователя root
- Выполните эту команду, чтобы перейти на Python 3.6 —
update-alternatives —install /usr/bin/python python /usr/bin/python3 1 - Проверьте версию Python — python —version
- Готово.
Создайте символическую ссылку для / usr / bin / python3. В моем LinuxMint:
Использование этих команд может помочь вам:
- проверьте версию python: ls /usr/bin/python*
- псевдоним: alias python=’/usr/bin/pythonxx’
- повторного входа в систему: .
/.bashrc
Простой ответ — добавить псевдоним для python3.6.
Просто добавьте эту строку в файл
/ .bashrc:, alias python3=»python3.6″ затем закройте свой терминал и откройте новый. Теперь, когда вы печатаете, python3 xxx он переводится в python3.6 xxx .
Это решение решает вашу проблему без необходимости слишком сильно настраивать вашу систему.
РЕДАКТИРОВАТЬ :
Как отметил Микаэль Кьер , это неверная конфигурация ANSIBLE в вашей системе.
Установите параметр ansible_python_interpreter конфигурации в / usr / bin / python3. Параметр конфигурации ansible_python_interpreter обычно устанавливается для каждого хоста в качестве переменной инвентаризации, связанной с хостом или группой хостов:
Как видно здесь о файле конфигурации:
Изменения могут быть внесены и использованы в файле конфигурации, который будет обрабатываться в следующем порядке:
Источник
Configure a Python interpreter
Python interpreters in PyCharm
To work with your Python code in PyCharm, you need to configure at least one Python interpreter. You can use a system interpreter that is available with your Python installation. You can also create a Virtualenv, Pipenv, or Conda virtual environment . A virtual environment consists of a base interpreter and installed packages.
With PyCharm Professional , you can also configure interpreters to execute your Python code on remote environments: SSH, Vagrant, WSL (only for Windows), Docker, and Docker Compose
When you configure a Python interpreter , you need to specify the path to the Python executable in your system. So, before configuring a Python interpreter, you need to ensure that you’ve downloaded Python and installed it in your system and you’re aware of a path to it. You can create several Python interpreters based on the same Python executable. This is helpful when you need to create different virtual environments for developing different types of applications. For example, you can create one virtual environment based on Python 3.6 to develop Django applications and another virtual environment based on the same Python 3.6 to work with scientific libraries.
Python interpreters can be configured for a new project or for the current project (you can create a new interpreter or use one of the existing interpreters).
Setting an existing Python interpreter
At any time, you can switch your Python interpreter either using the Python Interpreter selector or in the project Settings/Preferences .
Change the Python interpreter using the Python Interpreter selector
The Python Interpreter selector is located on the status bar. It is the most convenient and quickest way to switch the Python interpreter. Just click it and select the target interpreter:
Change the Python interpreter in the project settings
- Press Ctrl+Alt+S to open the IDE settings and select Project
Expand the list of the available interpreters and click the Show All link. Alternatively, click the icon and select Show All .
Select the target interpreter.
When PyCharm stops supporting any of the outdated Python versions, the corresponding Python interpreter is marked as unsupported.
When you change an SSH interpreter, you might need to synchronize local content with the target server. Mind a notification balloon in the lower-right corner. You can click one of the links to perform the following actions:
Auto-upload files to the server
Synchronize files and then enable auto-uploading
Modify a Python interpreter
- Press Ctrl+Alt+S to open the IDE settings and select Project
Expand the list of the available interpreters and click the Show All link. Alternatively, click the icon and select Show All .
Select the target interpreter and click Edit .
You can specify an alternative interpreter name for the selected interpreter. The Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes.
For remote interpreters (that are available in PyCharm Professional ) you can modify configuration parameters.
When you modify an SSH interpreter, PyCharm shows the parameters of the corresponding Deployment configuration . It is not recommended selecting the SSH option as it doesn’t allow file synchronization and is part of the legacy implementation.
Creating a new Python interpreter
To add a new interpreter to the current project:
Do one of the following:
Click the Python Interpreter selector and choose Add Interpreter .
Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project
| Python Interpreter . Then click the icon and select Add .
Choose the interpreter type to add and perform the specific settings:
Note that SSH, WSL, Vagrant, Docker, and Docker Compose are available only for the commercial version of PyCharm. WSL is Windows specific.
In the left-hand pane of the Add Python Interpreter dialog, select Virtualenv Environment . The following actions depend on whether the virtual environment existed before.
If New environment is selected:
Specify the location of the new virtual environment in the text field, or click and find location in your file system. Note that the directory where the new virtual environment should be located, must be empty!
Choose the base interpreter from the list, or click and find a Python executable in the your file system.
If PyCharm detects no Python on your machine, it provides two options: to download the latest Python versions from python.org or to specify a path to the Python executable (in case of non-standard installation).
Select the Inherit global site-packages checkbox if you want that all packages installed in the global Python on your machine to be added to the virtual environment you’re going to create. This checkbox corresponds to the —system-site-packages option of the virtualenv tool.
Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.
If Existing environment is selected:
Expand the Interpreter list and select any of the existing interpreters. Alternatively, click and specify a path to the Python executable in your file system, for example, C:\Python36\python.exe .
Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.
Click OK to complete the task.
If you have added the user base’s binary directory to your PATH environmental variable, you don’t need to set any additional options: the path to the pipenv executable will be autodetected.
Click OK to complete the task.
If you have not modified the PATH variable, PyCharm shows an error message: Pipenv executable is not found . Discover the proper executable path as described in the pipenv installation procedure and enter the target string in the Pipenv executable field, for example: C:\Users\jetbrains\AppData\Roaming\Python\Python37\Scripts\pipenv.exe (Windows) or /Users/jetbrains/.local/bin/pipenv (macOS).
Click OK to save the changes and complete the task.
In the left-hand pane of the Add Python Interpreter dialog, select Conda Environment . The following actions depend on whether the Conda environment existed before.
If New environment is selected:
Specify the location of the new Conda environment in the text field, or click and find location in your file system. Note that the directory where the new Conda environment should be located, must be empty!
Select the Python version from the list.
Specify the location of the Conda executable file in the text field, or click and find location in the Conda installation directory. You’re basically looking for a path that you’ve used when installing Conda on your machine.
Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.
If Existing environment is selected:
Expand the Interpreter list and select any of the existing interpreters. Alternatively, click and specify a path to the Conda executable in your file system, for example, C:\Users\jetbrains\Anaconda3\python.exe .
Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.
Click OK to complete the task.
In the left-hand pane of the Add Python Interpreter dialog, select System Interpreter .
In the Interpreter field, type the fully-qualified path to the required interpreter executable, or click and in the Select Python Interpreter dialog that opens, choose the desired Python executable and click OK .
If PyCharm detects no Python on your machine, it provides two options: to download the latest Python versions from python.org or to specify a path to the Python executable (in case of non-standard installation).
You will need admin privileges to install, remove, and upgrade packages for the system interpreter. When attempting to install an interpreter package through an intention action, you might receive the following error message: As prompted, consider using a virtual environment for your project.
Click OK to complete the task.
In the left-hand pane of the Add Python Interpreter dialog, click SSH Interpreter .
In the right-hand pane select New server configuration , then specify server information (host, port, and username).
In the next dialog window, provide the authentication details to connect to the target server.
Select Password or Key pair (OpenSSL or PuTTY) and enter your password or passphrase. If Key pair (OpenSSL or PuTTY) is selected, specify:
Private key file : location of the file with a private key
Passphrase : similar to a password, it serves to encrypt the private key.
The RFC 4716 format for OpenSSH keys is not supported by PyCharm. See the workaround.
Click Next to proceed with the final configuration step.
In the next dialog window, verify the path to the desired Python interpreter. You can accept default, or specify a different one. You have to configure the path mappings between your local project and the server. To do that, click next to the Sync folders field and enter the path to the local project folder and the path to the folder on the remote server.
You can also select the lowest checkbox to enable automatic upload of the local changes to the remote server.
In the left-hand pane of the Add Python Interpreter dialog, click Vagrant :
Click the browse button next to the field Vagrant instance folder , and specify the desired Vagrant instance folder.
This results in showing the link to Vagrant host URL.
The Python interpreter path field displays the path to the desired Python executable. You can accept default, or specify a different one.
Click OK . The configured remote interpreter is added to the list.
In the left-hand pane of the dialog, click WSL .
Select the Linux distribution and specify the path to the python executable in the selected Linux distribution. Typically, you should be looking for wsl.exe but you can specify any non default WSL distro.
In the dialog that opens, select the Docker option, from the drop-down lists select the Docker server (if the server is missing, click New. ), and specify the image name: python:latest .
Python interpreter path should have the default value:
Click OK to complete the task.
In the dialog that opens, select the Docker Compose option, from the drop-down lists select the Docker server, Docker Compose service (here web ), configuration file (here docker-compose.yml ) and Python interpreter path (here python ).
Why we’ve chosen web ? This choice is explained by the fact, that after configuring a Docker-Compose-based interpreter, we’ll be able to create regular run configurations that will alter the behavior of the container we selected. Therefore, if we want to debug the code in a container, that’s the one we should select here. All other containers in the compose file will always be started together with this one, but you won’t be able to affect their behavior from PyCharm — they’ll always behave as if you started them with the command docker-compose up from the command line.
Next, wait while PyCharm starts your Docker Compose configuration to scan and index:
Click OK to complete the task.
When a remote Python interpreter is added, at first the PyCharm helpers are copied to the remote host. PyCharm helpers are needed to run remotely the packaging tasks, debugger, tests and other PyCharm features. Next, the skeletons for binary libraries are generated and copied locally. Also all the Python library sources are collected from the Python paths on a remote host and copied locally along with the generated skeletons. Storing skeletons and all Python library sources locally is required for resolve and completion to work correctly. PyCharm checks remote helpers version on every remote run, so if you update your PyCharm version, the new helpers will be uploaded automatically and you don’t need to recreate remote interpreter. SFTP support is required for copying helpers to the server.
Setting the default interpreter
In PyCharm, you can specify an interpreter that will be automatically set for all newly created projects.
From the main menu, select File | New Projects Setup | Settings for New Projects (on Window and Linux) or File | New Projects Setup | Preferences for New Projects (on macOS).
Select Python Interpreter settings. Then either choose an existing interpreter from the Python interpreter list of click to add a new interpreter. Click OK to save the changes.
The change will become effective for all newly created projects in PyCharm.
Managing interpreter packages
For each interpreter, you can install, upgrade, and delete Python packages. By default, PyCharm uses pip to manage project packages. For Conda environments you can use the Conda package manager.
PyCharm smartly tracks the status of packages and recognizes outdated versions by showing the number of the currently installed package version (column Version ), and the latest available version (column Latest version ). When a newer version of a package is detected, PyCharm marks it with the arrow sign and suggests to upgrade it.
By default, the Latest version column shows only stable versions of the packages. If you want to extend the scope of the latest available versions to any pre-release versions (such as beta or release candidate ), click Show early releases .
You can upgrade several packages at once. Hold Cmd (macOS) or Ctrl on (Unix or Windows), left-click to select several items in the list of packages, and then click Upgrade .
Источник