I'd recommend using pyenv to manage your python versions. 3.5.0b3 is already added to pyenv.
Virtualenv is for isolating python versions, but it will not install them for you as /u/Southclaw implied.
Virtualenv is a tool that lets you set up self-contained Python installations in a folder, with their own python
binary and libraries/pip packages. This is useful for keeping separate Python applications isolated, so that updating the dependencies of one application doesn't affect the others.
In contrast, pyenv just allows you to easily switch between which version of Python is launched when you run python
(on a per-user basis). Most systems have both Python 2.x and 3.x installed, and some may also have 3rd-party interpreters such as PyPy, IronPython, etc.
EDIT: I talked about the difference between virtualenv and a project on GitHub called py*env*. /u/Rhomboid's comment discusses py*venv*, if you're wondering about that.
You can install and quickly switch python versions with pyenv. The install path can be basically wherever you want it. It also just installs python from source so you don't have to use the old versions from ubuntu.
I would recommend pyenv. It allows you to install different versions of python (including anaconda and pypy) into your home directory and easily switch between them on the command line.
I assume you mean that you have 2.7.6 installed via Homebrew?
Python 2.7 isn't receiving any new features, but it's still getting bugfixes and security updates, so I'd say it would be best practice to keep it updated.
I prefer to install different versions of Python to my home directory with pyenv. pyenv makes it dead simple to install and switch between versions, including (to my delight as a scientific coder) anaconda python distributions. I leave the system version of python alone, and I removed my homebrew python installation after finding pyenv.
As the sidebar says, if you are about to ask a question, please consider /r/learnpython
The best way (IMHO) to setup and use any number of Python versions on your computer is using pyenv. Have a look at virtualenv for isolated Python environments.
What you should be doing is using virtual environments. In conjunction with pyenv with pyenv-virtualenv you can create virtual environments based upon any version of Python you want: 2.7.9, 3.4.3, pypy, Jython, etc.
I highly recommend pyenv : it allows to install several versions of python (and virtual environments) alongside each other. It won't change the system python, and allows setting the python version based on what directory you're in in.
Example :
# Install python 3.6.0 pyenv install 3.6.0 # Create a python 3.6.0 virtual environment called "project-env" pyenv virtualenv 3.6.0 project-env # Create your project directory and cd into it mkdir myproject && cd myproject # Set the local python version to your newly create virtual environment pyenv local project-env
The last command creates a .pyenv
file in the directory - that's how pyenv will know to switch python version when you cd
into it.
You also have pyenv shell
that changes the python version in your current shell, or pyenv global
that changes the default python you'll be using.
I've been using it since i discovered it via a Pycon talk and can't recommend it enough !
I would recommend pyenv for Python management. You can install multiple versions of Python interpreter in your home directory without touching system's version. Then you can easily switch between them (pyenv global) or use different versions in different directories (pyenv local).
virtualenv is actually a python package that allows you to setup a clean environment for your project (for example if your system has a django 1.4 package and you want to use 1.7 in a project and 1.8 on a different one you can do that with two virtualenvs), if you want to use a python version different to the one you have on your system I would recommend pyenv with the pyenv-virtualenv plugin instead of just virtualenv.
Try to setup pyenv, if you need help after that I could give you a hand.
pyenv can be used to install different versions of python. See the documentation regarding the install command for details. For example, to install 3.3.6, you'd run
pyenv install 3.3.6
Once the version of python you'd like to use is installed, you can set it for your session using
pyenv shell 3.3.6
The various versions installed with pyenv are also available in the pyenv installation directory, typically ~/.pyenv/versions
I think you should clarify that by Pyenv you are referring to this: https://github.com/yyuu/pyenv
And not to pyvenv
(note the extra 'v') or venv
, which relate to the new built-in virtual environment tools (https://docs.python.org/3/library/venv.html) as I'm not sure that's going to be clear to everyone, based on /u/memilanuk's follow-up question.
> For this post, I'm going to assume that you're using OSX, and have Homebrew installed.
Or for everyone else (assuming you're using bash or something similar),
pip install --egg pyenv echo 'export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" ' >> $HOME/.profile
Or if you're on Windows, the author of pyenv doesn't care about you at all, sorry.
Once you have fixed your system Python, use pyenv in the future. It allows you to install a wide variety of Python versions and switch between them easily while keeping things quite clean. It's a good idea to leave the system Python as is and not use it for development/learning. Messing with it may break your OS.
You should use pip, preferably in a virtual environment so you don't screw up default packages and dependencies the system uses.
You should use virtualenv, virtualenvwrapper, or the more complex but useful <strong>pyenv</strong> which has virtualenv support in addition to being able to download standard python versions 2.4 through 3.4, pypy, stackless, jython etc. Pyenv also let's you do other things like specify which python version a directory should use, which version the shell should use, etc.
Consider where you need to have mutiple environments to test in. For example, with multiple django versions, you can just activate your virtualenv for django 1.4, test the server, then switch to a virtualenv with django 1.6 and test it under 1.6.
With standard virtualenv, you can place a virtualenv in any directory and to activate, you have to call the activate command of that virtualenv like this:
$ /path/to/virtualenv/activate # deactivate #to deactivate
With virtualenvwrapper, the location of the virtualenvs is managed for you, usually in the home folder, and you can just start a virtualenv by calling
$ workon virtualenv_1 #whatever you named it
Most Python programmers should use virtualenvs. As for why everyone doesn't use them? Good question. I know two other Python programmers offline and neither of them use virtualenv. To me, virtualenv is about how easy I want to be able to set up an environment. If I use the system and need the complete package requirements of a program, and I call
$ pip freeze > requirements.txt
to get the install requirements, I'm going to likely get a huge list of modules that are mostly irrelevent for the program I built if I've been using this approach for numerous previous programs. If I use a virtualenv for each program, then I get exactly which modules I need via pip freeze, and the correct versions.
Any distro you like
Most of the time the best distro to do <whatever you're trying to do> is the distro you are most comfortable with, there are usually very few exceptions although common sense is involved.
If you need specific python versions and/or you change frequently you can look at PyEnv or other python version management scripts
If you need other easy to use distros, these are some that pop up in my mind right now: OpenSuse, Linux Mint, all Ubuntu variants, Elementary OS, Fedora (not so easy though)
EDIT: realized using "X" as <generic something> is confusing in a linux context
My advice is to forget the dmg installer. First, install Homebrew. Using brew, you can install a variety of python versions.
Alternatively, you can use brew to install pyenv. Then use pyenv to install whatever Python version you like.
I realize this requires learning some new tools, but these tools are absolutely worth your time.
This doesn't answer your question directly, but I just wanted to recommend pyenv as being a really great tool if you want/need to manage multiple python versions on one machine.
It lets you do stuff like say "when I ask for python
use this version (maybe it's 2.7.1), but when I ask for python3
use this one (maybe it's 3.5.2), oh and in this directory when I ask for python
use this other version instead (maybe it's 3.4.1), etc. Apparently it works well with anaconda too though I haven't tried that.
Not really, pyenv
builds all versions (the ones it knows about anyways) of CPython from source. The packages which OP installs from apt-get
are listed as dependencies in the pyenv wiki.
pyenv
can also easily manage which of your python versions is active, without having to manually symlink stuff.
I use pyenv and it's virtualenv plugin to manage my python environments. You can use it to switch between installations and virtual envirnments, plus install from a list of 216 python versions, including 2.x and 3.x from the standard CPython distribution, or anaconda, miniconda, pypy, jython, stackless.
Have a look at pyenv. It lets you manage and switch between multiple versions of Python.
As long as you don't remove old versions when upgrading, existing virtual environments won't break.
what's your environment? if you're on anything Unix based I'd suggest using pyenv + virtualenv, it'll handle installing pip for the right version of python
I use pyenv, installed through brew
. There's currently 190 versions you can install through it, including 2.1.3 through 3.5.0, a dev version for 3.6, pypy, jython, anaconda, and others. There are also virtualenv plugins available within the tool. To install a new python version, all you have to do is pyenv install [version]
To be honest, I go with pyenv and pyenv-virtualenv.
Lets me have multiple versions of Python installed, choose which one I want when setting up a virtualenv, etc.
So if I'm setting up a new project and want to test it on 2.7 and 3.5, I can do
pyenv virtualenv 2.7.10 newproject27 pyenv virtualenv 3.5.0 newproject35
Now I have two virtualenvs I can use: newproject27
will have Python 2.7.10, and newproject35
will have Python 3.5.0.
CPython 2.7.10 is available in the latest version in the latest pyenv. We usually manage to get new versions of CPython added the same day they are released.
> never mind using tools like pyenv, etc
For Python, at least, pyenv
is actually incredibly simple.
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv $ git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper
This is the snippet of my dotfiles that activates my installation of pyenv
with the pyenv-virtualenvwrapper
plugin. (I've pulled out some special-casing for situations where I don't have pyenv
installed and some error reporting.)
if test -d ~/.pyenv; then export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)" if test -d ~/.pyenv/plugins/pyenv-virtualenvwrapper; then pyenv virtualenvwrapper_lazy fi fi
See available versions:
$ pyenv install -l
Install cpython
2.x and 3.x:
$ pyenv install 2.7.9 $ pyenv install 3.4.3
Then issue
$ pyenv global 2.7.9:3.4.3
This will write those two versions to ~/.pyenv/version
. Having both versions active (and in that order) will produce behavior similar to how the system-wide packages behave in a distro that defaults to Python 2: python
will invoke your local copy of 2.7.9, and python3
(or python3.4
, etc.) will invoke your local copy of 3.4.2.
I would suggest not changing the order, even if you tend to use Python 3; you're changing your PATH
, remember, so all sorts of things (usually the cruftiest things around) will invoke python
and explode... or just misbehave in interesting ways.
(Sorry to only answer part of your question. There are plenty of tricks to make locally-installed binaries convenient, too; but this is already a very long comment!)
I tend to use pyenv with pyenv-virtualenv. With that, you can do
pyenv virtualenv venv
or
pyenv virtualenv <Python version> venv
And you have venv
treated by pyenv
as another Python installation that you can switch to/switch from/uninstall at will.
I use pyenv to manage my python version and then have separate virtualenvs for each project.
https://github.com/yyuu/pyenv/blob/master/README.md#homebrew-on-mac-os-x
It's possible pip just isn't on your system path. Have you tried calling pip with the full path? This will tell you if this is the problem.
I'd recommend anyone using python on Linux or Mac to use pyenv available at https://github.com/yyuu/pyenv
It makes things so much easier.
Check out pyenv. Easy to install, works like a charm and you can have multiple versions of Python (from a choice of 147 - includes stackless, pypy, anaconda, etc.) on your system.
Get most any Python version, including 2.4 to 3.4, pypy, stackless, etc. by using this commandline tool -> https://github.com/yyuu/pyenv. It can also handle virtualenvs.
If you don't want something so difficult to use, you can get homebrew and brew install python3.
If it's not an issue with Python version, it might be a tabs/spaces issue. Some windows editors default to \t when you hit tab instead of spaces in .py files. If you have a text editor like Sublime Text (you can get it for free), do a command-option-f in the open file and you can replace all instances of \t with spaces if you turn on regex on.
pyenv if you are on OS X or Linux. And there is also homebrew just for OS X. I personally use homebrew because I don't need to test older Python versions. So I have Python 2.7.8 and Python 3.x.
Updating them is as simple as brew upgrade.
On Windows, Python 3 installs it into a separate folder. Then just modify your path to point to the new one.
I develop things which need to run on 2.6, 2.7, 3.3 and 3.4.
I use pyenv for that; it's a tool which makes it easy to install multiple versions of Python, and create virtualenvs which will use specific versions.
I use this setup on my Debian Wheezy:
No manual install of python 3.2 is necessary, Pyenv handles it all for you. In fact, you can get any version of Python from 2.4 to 3.4, including Pypy, Jython, Stackless, etc. You can also create virtual environments with it or specify a version of python to be used on a directory level so you don't have to type anything but python each time.
I've written a few things with praw using python 2.7.x, and more recently, 3.3.0. Strings are unicode by default in Python3, so you shouldn't get errors like the one you're asking about.
If you're not already using it, I'd look into pyenv and its virtualenv plugin. Pyenv makes using multiple versions of python simple, and virtualenv is virtualenv.
You need to set up ssh for the jenkins user that will do the install, just run ssh-keygen for that user, then you need to add some settings to its ~/.ssh/config, below is how I install pyenv in my containers. (I like to have pyenv so I easily can write `pyenv install 3.8.1` when testing):
RUN mkdir -p ~/.ssh RUN echo "Host *\nStrictHostKeyChecking no\nPasswordAuthentication yes\n\n" > ~/.ssh/config
RUN git clone https://github.com/yyuu/pyenv.git $PYENV_ROOT RUN git clone https://github.com/yyuu/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv RUN git clone https://github.com/pyenv/pyenv-update.git $PYENV_ROOT/plugins/pyenv-update
An easier way to install this is to use pyenv. Just clone the source, activate it in your bashrc or zshrc and then type
CFLAGS="-O2" PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.6.0 pyenv global 3.6.0 # to switch to using 3.6.0 globally
I highly recommend pyenv and pyenv-virtualenv to manage multiple CPython (and other) versions. Invaluable for testing your code on multiple Python versions!
It shouldn't matter if you've got old versions lying around. If you're ever in doubt, use the which
command to see what's running. Also pyenv is insanely helpful and I recommend it for managing virtualenvs.
Many thanks for your suggestions!
Adding/updating a lot of lines in a file seems a bit of a pain in Ansible, although I have just noticed the extras blockinfile module which would do the trick, so maybe I'll do that with the activate
script.
I haven't looked at using pyvenv
- I understand that's only with Python34+? I'm using <code>pyenv</code>.
While I agree that conda is the way to go, maybe I'm missing something, but isn't what you are asking about one of the primary purposes of pyenv. Or maybe I'm not sure what you mean by "create".
See their command reference
It points to python-build, which is a separate, but associated project that installs lots of different python versions. It can also install anaconda/miniconda and pypy. The thing I like about pyenv is that it doesn't require anything but bash to bootstrap your install. The thing that sucks about it compared to conda is that it will mostly be compiling from source, so you need all the dependencies, plus it takes some time.
So pyenv manages these python installs, then also can change which python your current shell is using or a project uses via config files. Virtualenv I believe is more for when you already have python available on your system and you want a contained environment for a specific app to install dependencies into.
yes, pyenv: https://github.com/yyuu/pyenv
pyenv installs the python version you want, say 3.5.
Py2 is your system default. You enter a new projects where you need python 3. You type: pyenv local 3.5
and you're done !
.
Update 2
Got it to work with that.
Thank you!
Update
I think I an issue regarding it and a solution,
https://github.com/yyuu/pyenv/issues/429
Original
I'm having issues with
pyenv install 2.7.10 -v
It's giving me problems regarding zlib and checking the history, I can see some issues with X11. I checked and saw a caveat in the Wiki. Basically I need to point to the correct headers using,
CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 2.7.7
But I still haven't managed to get it to work.
Did you by any chance came across this? I'm going to keep trying anyway and post in their issue queue if not...
/usr/local/bin/pip freeze
. Normally what you could do is pip freeze
, but if pip list
doesn't list the packages, pip freeze
won't either.Until someone has a better answer you could try this:
You can reduce that package list with this:
reqs = [] ignore_list = ['dist-info', 'egg-info', 'pyc', 'pth'] ignore_list_line = ['virtualenv.py', 'virtualenv_supprt'] with open('site-packages-list.txt') as f: for line in f.readlines(): line = line.strip() if line.strip().split('.')[-1] not in ignore_list and \ line not in ignore_list_line: reqs.append(line) # or print out print line
That'll reduce it to 52 lines, some containing .py at the end. Make a file with python filter_reqs.py > some_reqs.txt
. You can look through it and delete the .py
parts, and some other things which maybe shouldn't be there like decorator.py. Assuming the file is good to go, you can do this to install the requirements from the requirements file: pip install -r some_reqs.txt
.
Maybe pyenv can help you, but I'm not sure because I've never used it.
When I had this problem, I just changed the PATH variable, so that Python 3 appeared first in the list. In Windows 8 you can find and change the PATH variable in: Start -> All Apps -> Control panel -> System -> Advanced System Settings -> Advanced -> Environment variables. The values/directories are delimited by semicolons C:\python34;C:\python27
. However, I've hardly ever used Python 2 and I guess if you have to switch it very often that'll be quite annoying.
Pre-release versions are usually only available until the final version is released. Alphas are available until the first beta, betas are available until the first release candidate, etc.
There's no pyenv release that includes 3.5.0b3 yet, so to get it you will have to install pyenv as described here.
pyenv
will isolate different versions of python from the system version, but it isn't useful for isolating different project environments. You can use virtualenvs in conjunction with pyenv-virtualenv to ease the process, but it's pretty easy to do manually anyways.
Why do virtual box again? Just to be able to move it to servers? You know you can clone environments with virtualenvwrapper or pyenv? You dont need to run all that overhead to simply isolate dev environments.
You'll need shell access to create the app and install libraries from pip. You can also find a hosting provider that uses Phusion Passenger to deploy Python-backed apps. Having access to pyenv is handy too, to switch between Python versions other than what is the system default.
That's how we do it at least.
If you're switching to Python 3 for software development, use pyenv: https://github.com/yyuu/pyenv
This downloads and installs any version of Python to (by default) your home directory, and you can switch between them easily. It's very nice, and disconnects your system use of Python from your personal use of Python -- which is a good thing.
For a beginner, I do not recommend using Homebrew to install and manage different versions of Python. Pyenv makes it very simple to install, manage and switch versions. The out-of-the-box functionality will keep you happy for a long time. Additionally, you can use pyenv-virtualenv to create and manage virtualenvs as well. Good tutorial here.
I suggest you learn more about Homebrew through the documentation (which is quite good).
As for learning Git, understand that you don't have to know everything to start. It's not a straight line that we walk for a mile. You learn as you go. Start with simple, barebones, basic stuff and make it a habit; progressively learn more as you need it. Here is a barebones Git tutorial.
My apologies for the late answer. We live in different timezones, I believe.
The default python installation has always worked for me on mac. But I use pyenv to manage my python interpreters and virtual environments.
You can get any distribution of python you want - anaconda, stackless, pypy, jython, or the default from 2.4 to 3.4.
Why not just use the python2.7 package if you're gonna do a global install? That said, I'd suggest using yyuu instead. Automates Python compiles and also supports stuff like per-project or per-shell-window Python version selection.
"the question is specifically how to get python 3.4, which is not distributed with ubuntu yet, installed into ubuntu." Thats exactly what I use pyenv for, I run Ubuntu 12.04, and I have python 2.6.6, and 3.4.0 installed on my laptop using pyenv. I think you have the wrong understanding of how pyenv works.
Please follow the install instructions (https://github.com/yyuu/pyenv)
A picture is worth a thousand words (screenshot of terminal)[http://imgur.com/mAOwkEj]
Edit: I re-read your post and it occurred to me to add the following:
The thing to keep in mind is that pyenv works by manipulating your shell env. This means you cant use the installed python3.4 the usual way (e.g $ python3 ), you need to tell pyenv to switch to the "installed" version you want. (see the local, and global commands)
After you have used pyenv to install python 3.4, tools like virtualenvwrapper, pip, and virtualenv wont just work (if you previously had them installed) To get them to work, you need to make python 3.4 global (pyenv global 3.4.0) then re-install, virtualenv, and virtualenvwrapper. Once done you can then switch back and forth with no issues or conflicts.
For getting a Python development (not end user) environment up and running, I would currently recommend pyenv. Your step-by-step guide.
curl https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
(Note that this installs in ~/.pyenv
by default)
Add this line to your bash or zsh startup: eval "$(pyenv init -)"
Restart your shell (exec
it, or log out and back in)
pyenv install 3.4.0
(Wait while it builds and installs)
pyenv virtualenv 3.4.0 devel
(Create a new virtualenv called devel
, based on 3.4.0)
pyenv shell devel
(Switch to the devel
virtualenv)
pip install flask django
pyenv can do a lot more; I suggest reading the documentation.
I also really like Anaconda, as mentioned by /r/rainnz, but I find it more appropriate for end users (especially in scientific computing) rather than developers. That said, I also use it to maintain my own "end user" Python installation, while using pyenv for when I need to develop using the language.
If you want a conda-based Python environment and you don't want the entire Anaconda distribution, install using Miniconda.