I am researching possibility to upgrade to Python 3.6 in our project.
Right now we are using Python 3.5.2 from ppa:fkrull/deadsnakes on Ubuntu 14.04. The PPA doesn't have Python 3.6 yet and it's not clear when it will be available.
I don't want to install yet another PPA.
And I am trying to find a more general approach.
I found people suggesting to use pyenv which compiles Python from source, which sounds interesting, because I can upgrade Python any time without waiting until repo maintainer adds it. Also I can easily install other Python flavors like PyPy.
I am not ready to use pyenv as virtual environment yes, so I am wondering if it's possible to use it to compile and install Python globally so that I can just use it.
The documentation is a little confusing because there is no python-build binary added in PATH after installation.
python-build is a pyenv plugin (installed by default). Documentation and more info is here: https://github.com/pyenv/pyenv/tree/master/plugins/python-build.
How to install system-wide Python for all users: 1) Login as root and 2) install required Python version to /usr/local/python-X.Y.Z.
sudo ~/.pyenv/plugins/python-build/bin/python-build 3.6.1 /usr/local/python-3.6.1/
Now you can use this Python version as a normal user, for example you can create virtualenv for your project:
/usr/local/python-3.6.1/bin/python -m venv /var/www/my-app/.env/
https://github.com/yyuu/pyenv/wiki/Common-build-problems#installing-a-system-wide-python
Installing a system-wide Python
If you want to install a Python interpreter that's available to all
users and system scripts (no pyenv), use /usr/local/ as the install
path. For example:
sudo python-build 3.3.2 /usr/local/
I've contributed a package for python3.6 in deadsnakes for trusty / xenial :)
https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes/+packages?field.name_filter=python3.6&field.status_filter=published&field.series_filter=
By combining the hints from the other answers and reading through the documentation, I found a nice way to do exactly what you want that should work well in a CI system or in a Docker container or on a developer machine if they haven't already installed python3.x via Apt or Yum or Homebrew.
Assuming you have all the dependencies required to build your desired version of Python 3.x (anything above 3.4 requires some extra packages the pyenv-installer doesn't always warn you about), you can run the commands below to get a new system wide Python that should be executable by all users, which makes it easy to pass to virtualenv creations with python3.6 -m venv yourvenv.
curl https://pyenv.run | bash # or
wget -O - https://pyenv.run | bash
export PATH="$HOME/.pyenv/bin:$PATH"
$(pyenv which python-build) 3.6.10 /usr/local/
which python3.6
python3.6 --version
# If you get an error running the above commands, it probably means
# /usr/local/bin isn't in your PATH yet
# on Debian/Ubuntu and maybe others the /etc/environment or
# /etc/login.defs file puts it in the path when a user logs in
echo $PATH
export PATH="/usr/local/bin:$PATH"
python3.6 --version
Related
I'm trying to install an older version of tensorflow and it needs python3.6 to support the whl file of installation.
I'm now running Ubuntu 20.04 with python 3.8.5, I've already done this :
sudo add-apt-repository ppa:deadsnakes/ppa
followed by :
sudo apt-get update
sudo apt-get install python3.6
By doing that it installs python 3.6 but when I see the version of python installed it's still 3.8.5. Should I do something to remove python3.8.5 ? maybe apt-get purge ?
P.S: I'm installing the wheel file through pip3 should I downgrade it too ?
You shouldn't hack the system installation of Python I'd say.
Instead you should use something that let's you manage multiple versions of it, something like pyenv.
It is a well-known and widely accepted utility and according to its readme:
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.
You can find the installation instructions here: https://github.com/pyenv/pyenv#basic-github-checkout.
Once you installed it you can install your preferred Python distribution which will live besides your system one and all the others you want later.
E.g. if you want to install 3.6.7 and then use it globally you can do:
pyenv install 3.6.7
pyenv global 3.6.7
Then test it like:
python -V
And that's only to scratch the surface, you can do many more things with it, check out the documentation for more.
after the commands you've run, you should have a python3.6 binary installed on the path
that said, I'd recommend using virtualenvs instead of system installations
for deadsnakes, you can install the venv module by (sudo) apt install python3.6-venv (debian decided to split venv into a separate module, so the deadsnakes packaging follows that)
from there you can create and activate a virtualenv:
python3.6 -m venv venv # create the environment
. venv/bin/activate # activate the environment
pip install ... # install things to your isolated environment
inside this virtualenv the python command will refer to your own isolated python installation
disclaimer: I'm the maintainer of deadsnakes
I recently started using elementary OS. It's based on Ubuntu.
During installation it installs python 3.6.
I installed python 3.8 manually by installing the following packages: python3.8, python3.8-dev, python3.8-minimal, python3.8-venv.
I also updated the link to the python binary with:
sudo ln -sf /usr/bin/python3.8 /usr/bin/python3
After this a couple of things stopped working.
For example when I tried to execute a non-existing command it didn't print the error message that it cannot found the command, but it displayed a python stack trace. That one I solved with:
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
sudo ln -s apt_inst.cpython-36m-x86_64-linux-gnu.so apt_inst.so
As you can see the python error was because it couldn't find the compiled binaries for the apt module.
So this issue was solved, but there are a couple of similar ones, and none of them can be solved this way, as the module binary is not compatible with python 3.8.
Is it possible to remove python 3.6 completely and override it with 3.8 so that the module binaries also get updated? Or can python 3.8 and 3.6 coexist? I would be fine with the /usr/bin/python3 link pointing to python 3.6 and I would manually execute /usr/bin/python3.8 or create a different alias or link for it. However when I print out the sys.path with /usr/bin/python3.8 I get this:
/usr/lib/python38.zip
/usr/lib/python3.8
/usr/lib/python3.8/lib-dynload
/home/{username}/.local/lib/python3.8/site-packages
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
The trouble is with the last one. That's where the modules are with the 3.6 compatible so files. Can I somehow force python 3.8 to completely ignore the last module search path (without always stating sys.path.remove in my scripts) ?
I recommend you undo your symlink overwriting your systemwide version of Python 3. As you have found, replacing the python3 executable might create some problems, as it is used under the hood.
In general, you should leave your system python[3] installation alone for this reason and it is common to use virtual environments. This can be done as follows (assuming you are in some project directory):
python3.8 -m venv venv
source venv/bin/activate
The first command will create a virtual environment ('venv') in the directory venv. The second command will 'activate': now python (and in this case, python3 and python3.8) all refer to your original python3.8 in the context of this shell. You will have to repeat this if you launch a new shell.
This will also allow you to install packages using pip without cluttering your system installation. The use of virtual environments and pip is an incredibly common workflow in the Python development world.
In terms of shell and 'global' Python management, you can also use pyenv to manage your Python versions and what is available in the shell. pyenv is quite nice if you want to run a particular version of python, say 3.8.0 but not 3.8.1.
Presently we have a big-data cluster built using Cloudera-Virtual machines. By default the Python version on the VM is 2.7.
For one of my programs I need Python 3.6. My team is very skeptical about 2 installations and afraid of breaking existing cluster/VM. I was planning to follow this article and install 2 versions https://www.digitalocean.com/community/tutorials/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4
Is there a way "I can package Python 3.6" version in my project, and set the Python home path to my project folder, so that there is no installation that needs to be done on the existing Virtual machine?
Since we have to download python and build source for the Unix version, I want to skip this part on VM, and instead ship the folder which has Python 3.6
It seems that miniconda is what you need.
using it you can manage multiple python environments with different versions of python.
to install miniconda3 just run:
# this will download & install miniconda3 on your home dir
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh -b -p ~/miniconda3
then, create new python3.6 env:
conda create -y -n myproject 'python>3.6'
now, enter the new python3.6 env
source activate myproject
python3
miniconda can also install python packages, including pip packages and compiled packages. you can also copy envs from one machine to another. I encourage you to take a deeper look into it.
ShmulikA's suggestion is pretty good.
Here I'd like to add another one - I use Python 2.7.x, but for few prototypes, I had to go with Python 3.x. For this I used the pyenv utility.
Once installed, all you have to do is:
pyenv install 3.x.x
Can list all the available Python variants:
pyenv versions
To use the specific version, while at the project root, execute the following:
pyenv local 3.x.x
It'll create a file .python-version at the project root, having the version as it's content:
[nahmed#localhost ~]$ cat some-project/.python-version
3.5.2
Example:
[nahmed#localhost ~]$ pyenv versions
* system (set by /home/nahmed/.pyenv/version)
3.5.2
3.5.2/envs/venv_scrapy
venv_scrapy
[nahmed#localhost ~]$ pyenv local 3.5.2
[nahmed#localhost ~]$ pyenv versions
system
* 3.5.2 (set by /home/nahmed/.python-version)
3.5.2/envs/venv_scrapy
venv_scrapy
I found it very simple to use.
Here's a post regarding the installation and basic usage (blog post by me).
For the part:
Since we have to download python and build source for the Unix
version, I want to skip this part on VM, and instead ship the folder
which has Python 3.6
You might look into ways to embed Python interpreter with your Python application:
And for both Windows and Linux, there's bbfreeze or also pyinstaller
from - SOAnswer.
The scenario is: I am on Ubuntu 11 which comes with Python 2.7, I want to run Mozilla JetPack which supports Python 2.5/2.6 and Google App Engine which only supports Python 2.5.
Read that its not a good idea to remove Python 2.7 as Ubuntu maybe using it. So the correct way is to use virtualenv. But I am quite lost using it. I installed Python 2.5 in /usr/local/python25 following this guide
I tried
jiewmeng#JM:/usr/local/python25/bin$ ./python --version
Python 2.5.5
jiewmeng#JM:/usr/local/python25/bin$ ./python virtualenv /works/tmp/test
./python: can't open file 'virtualenv': [Errno 2] No such file or directory
then the below works but I will be using Python 2.7
jiewmeng#JM:/usr/local/python25/bin$ virtualenv /works/tmp/test
New python executable in /works/tmp/test/bin/python
Installing distribute.................................................................................................................................................................................done.
jiewmeng#JM:/usr/local/python25/bin$ cd /works/tmp/test/bin
jiewmeng#JM:/works/tmp/test/bin$ ls
activate activate_this.py easy_install easy_install-2.7 pip python
jiewmeng#JM:/works/tmp/test/bin$ ./python --version
Python 2.7.1+
Also, how do I then run Mozilla JetPack or Google App Engine with this version of Python? Sorry I am new to Python (and Linux/Ubuntu)
Outline:
First cd to /usr/local/python25/bin
Download setuptools for Python2.5 (setuptools-0.6c11-py2.5.egg)
Install it (sh setuptools-0.6c11-py2.5.egg).
Now install pip (easy_install pip).
Install virtualenv and virtualenvwrapper using pip (pip install v... etc.).
Configure WORKON_HOME for virtualenv wrapper to work (export WORKON_HOME = $HOME/.virtualenvs). You can use any other directory you want (not just $HOME/.virtualenvs). Just make sure to use the full path.
Now create a virtualenv (mkvirtualenv foobar).
Switch to the new virtualenv (workon foobar).
Now install GAE, JetPack and whatever you want using pip install blah
Why did your install not work?
Looks like you did not install virtualenv for Python2.5. Hence this will not work.
jiewmeng#JM:/usr/local/python25/bin$ ./python virtualenv /works/tmp/test
You can check by running ls command in that directory. I suspect you won't find virtualenv file there.
However this worked for you.
jiewmeng#JM:/usr/local/python25/bin$ virtualenv /works/tmp/test
Because it is using the virtualenv file for system default Python2.7. You can check which virtualenv and opening the virtualenv script. You'll see that the #! will point to system default python.
So you need to install the easy_install and pip for Python 2.5 before you can create virtualenv for Python 2.5. Just follow the steps outlined above.
You don't need to do anything fancy outside the virtualenv wrapper. Just use the --python=python2.5 flag (check out the man page for virtualenv form more). It does not matter what version you install it with, you just have to select the right executable for python in the virtual environment.
e.g. mkvirtualenv --python=python2.5 --distribute python25 if the python flag fails, either add a symlink (ln -s) to python25 in your $PATH or use the full path name on the python flag.
Also, default for multiple python installations is to have, for all 'altinstall' versions, a separate python and easy_install. So, for example: python2.5 ,easy_install-2.5 ,python2.6, easy_install-2.6 etc.
Story:
One of the app that i have works on python 2.4 and other on 2.6. I tried to do a sym link of python2.4 to python and things started to break loose on ubuntu jaunty.
Now i am downloading every dependency of 2.4 and installing it using python2.4 setup.py install. The dependencies seem to be endless.
Question1: How will i tell any framework that go and use version so and so pf python like day django to use 2.6 and say mjango to use 2.4? Something like we say use database databasename kinda syntax.
Question2: Is there more elegant way to switch between version as my hack of symlinking was a virtual disaster?
Question3: Can I download a deb for say hardy and make jaunty believe its for her?
Use Virtualenv.
There is more information here: Working with virtualenv.
Using virtualenv you can create a new virtual python environment with whatever version of Python you want for each project or application. You can then activate the appropriate environment when you need it.
To expand on my answer:
You can install multiple versions of Python on your computer (I have 2.4, 2.5, 2.6 and 3.1 on my machine - I install each from source). I use a Mac, and keep my system Python as whatever OS X sets as the default.
I use easy_install to install packages. On ubuntu you can get easy_install like this:
sudo apt-get install python-setuptools
To install virtualenv then do:
easy_install virtualenv
I tend to create a new virtualenv for each project I'm working on and don't give it access to the global site-packages. This keeps all the packages tight together and allows me to have the specific versions of everything I need.
virtualenv -p python2.6 --no-site-packages ~/env/NEW_DJANGO_PROJECT
And then whenever I am doing anything related to this project I activate it:
source ~/env/NEW_DJANGO_PROJECT/bin/activate
If I run python now it uses this new python. If I use easy_install it installs things into my new virtual environment.
So, virtualenv should be able to solve all of your problems.
Pythonbrew is a magical tool. Which can also be called as Python version manager similar to that of RVM-Ruby version manager but Pythonbrew is inspired by Perlbrew.
Pythonbrew is a program to automate the building and installation of Python in the users $HOME.
Dependencies – curl
Before Installing the Pythonbrew, Install “curl” in the machine, to install curl use the below command in the terminal, give the the password for the user when prompted.
$sudo apt-get install curl
After Installing the curl, Now Install Pythonbrew, copy and paste the following commands in the terminal and type the password for the user when prompted.
Recomended method of installation - Easy Install
$ sudo easy_install pythonbrew
To complete the installation, type the following command
$pythonbrew_install
Alternate method of installation:
Use curl command to download the latest version of pythonbrew from github.
curl -kLO http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install
After downloading, change “pythonbrew-install” to “executable”
chmod +x pythonbrew-install
Then, run the pythonbrew-install in the terminal
./pythonbrew-install
Now the Pythonbrew has been installed in the “Home Directory” i.e., /home/user/.pythonbrew
Next, copy and paste the following line to the end of ~/.bashrc
*NOTE: change “user” to your user name in the system
source /home/user/.pythonbrew/etc/bashrc
Thats it! Close the terminal.
Steps to Install different versions of Python:
Open a new terminal, type the following command or copy and paste it.
$pythonbrew install 2.6.6
This will install Python 2.6.6 and to install Python 2.7 or Python 3.2, change the version number in the previous command.
$pythonbrew install 2.7
or
$pythonbrew install 3.2
Update: If you get error while Installing then Install using the below command.
$pythonbrew install --force 2.7
or
$pythonbrew install --force 3.2
How to manage different versions of Python installed in system
For instance, if Python 2.6.6, Python 2.7 and Python 3.2 is installed in your system, switching between the versions can be done as follows:
By default, Python 2.6.6 will be active and in order to switch to Python 2.7 use the below command
$pythonbrew switch 2.7
The default Python is changed to Python 2.7.
Now, to switch to Python 3.2 change the version number in the previous command.
$pythonbrew switch 3.2
Use the below command to check or list the installed Python versions
$pythonbrew list
Use the below command to check or list the available Python Versions to install
$pythonbrew list -k
To uninstall any of the installed Python version (for example to uninstall Python 2.7), use the below command.
$pythonbrew uninstall 2.7
Use the below command to update the Pythonbrew
$pythonbrew update
Use the below command to disable the Pythonbrew and to activate the default version
$pythonbrew off
Enjoy the experience of installing multiple versions of Python in single Linux / ubuntu machine!
I find http://github.com/utahta/pythonbrew much easier to install and use than any other solution.
Just install it and you'll have these options:
pythonbrew install 2.7.2
pythonbrew use 2.7.2 # use 2.7.2 for a current terminal session
pythonbrew switch 2.7.2 # use 2.7.2 by default system wide
pythonbrew uninstall 2.7.2
Note: if you're using a Linux-based operating system with preinstalled Python, switching (system wide) to another version can make things go wrong, so be careful.
A more grassroot approach than Virtualenv is the side-by-side installation of two Python versions.
If there is an existing installation, and you want a second installation into the same root path (e.g. /usr/local), use this target when making install:
make altinstall
When your second installation is Python 2.6, this will leave you with a /usr/local/bin/python2.6 alongside the old /usr/local/bin/python.
A simple way to switch between these two versions is using a shell alias (alias python=/usr/local/bin/python2.6) on the shell where you invoke the interpreter. But this won't work across sub-shells and she-bang invocations.
pyenv is yet another Python manager. The README.md at that link has a good set of instructions, but they basically are:
$ cd
$ git clone git://github.com/yyuu/pyenv.git .pyenv
Then set up your $PATH.
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Install the desired versions of Python:
$ pyenv install 2.7.8
After installing you need to run this:
$ pyenv rehash
Then switch to the version of Python you want to run, for the shell:
$ pyenv shell 2.7.8
"Question1: How will i tell any framework that go and use version so and so pf python like day django to use 2.6 and say mjango to use 2.4?"
You simply run them with the specific python version they need. Run mjango with /usr/bin/python2.4 and django with /usr/bin/python2.6. As easy as that.
"Question2: Is there more elegant way to switch between version as my hack of symlinking was a virtual disaster?"
Yes, see above. Have two separate installs of Python, and run explicitly with the different versions.
"Question3: Can I download a deb for say hardy and make jaunty believe its for her?"
That generally works. If it doesn't, it's because it has dependencies that exist in Hardy, and does not exist in Jaunty, and then you can't.
And here is a Question 4 you didn't ask, but should have. ;)
"Is there an easier way to download all those Python modules?"
Yes, there is. Install setuptools, and use easy_install. It will not help you with library dependecies for those Python modules that have C code and need to be compiled. But it will help with all others. easy_install will download and install all the Python dependencies of the module in question in one go. That makes it a lot quicker to install Python modules.
Move to the project directory :
Create an environment :
virtualenv -p python2.7 --no-site-packages ~/env/twoseven
Then activate your source :
source ~/env/twoseven/bin/activate