Python - Virtualenv , python 3? - python

Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to have python 3.1.1 with subversion pygame to write my own stuff, and python 2.6 with normal stable pygame to use to run other things, like pygame games downloaded from pygame.org. Any help on how to accomplish that? Thanks.
OK I realized virtualenv is not what I'm looking for.

It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.
As I understand it you want to create different virtual environments with different Python versions?
This is very easy, and you only need virtualenv itself.
For, say, a Python 3:
$ virtualenv -p python3 p34env
(...)
New python executable in p34env/bin/python3.4
Also creating executable in p34env/bin/python
Installing setuptools, pip...done.
$ source p34env/bin/activate
(p34env)$ python -V
Python 3.4.2
(p34env)$ deactivate
$
You use the source command to activate the venv, and deactivate to - you guessed it - deactivate it. Notice the prompt changes to indicate the env.
For your system's standard version of Python you just skip the -p python3 argument, and you can use the argument to point to any version you want given a path.
The last argument is the name (p34env) and you can make as many as you like, just give them different names.

Your use case doesn't actually need virtualenv. You just need to install several different Python versions.

virtualenv is designed to create isolated environments of a Python environment. The trick to using it with multiple Python instances is to either install virtualenv into each of the Python versions you want to use it with, for example:
/usr/bin/easy_install-2.6 virtualenv
/usr/local/bin/easy_install virtualenv
sudo port install py26-virtualenv
or to invoke it with the intended Python version, for example:
/usr/bin/python2.6 virtualenv.py ENV
/usr/local/bin/python2.6 virtualenv.py ENV
/opt/local/bin/python2.5 virtualenv.py ENV
So, as such, it doesn't directly solve the problem (particularly acute on OS X) of which Python you want to work with. There are various ways to deal with that issue: use absolute paths to the intended Python (as in the above examples), define shell aliases, carefully manage the $PATH search order, among others.
At the moment, AFAIK, virtualenv is not supported with Python 3 because, among other things, setuptools (the magic behind easy_install) is not yet supported on Python 3, although there is work in progress towards a solution for that.
BTW, many people use Doug Hellman's virtualenvwrapper to simplify use of virtualenv.

For me virtualenv3 works very well. I also have pypi.python.org/pypi/distribute installed. That also works with the mentioned www.doughellmann.com/docs/virtualenvwrapper/ . I've only tested that on Linux though.

Not sure if I understood you correctly, but here goes :)
I don't know about OS X, but in Linux you can install both 2.6 and 3. Then you can either specify to use python25 or python3, or change the /usr/bin/python symlink to the version you want to use by default.

Related

a couple Python versioning questions (touching Homebrew and venv)

I read the Homebrew notes on Python but I'm still a bit confused based on a couple specific situations described below. I'm also starting to use venv and a bit confused how that works. So here are a couple questions regarding how both of those handle Python versioning. Thanks in advance.
brew list shows both python3 and python. This is a bit confusing to me because I have also read that Python 2 is referred to as python#2, and I don't think I have Python 2 installed. So I'm wondering why there are 2 different formulae which both seem to mean "Python 3". (EDIT: I might be able to answer this question myself, I just found another mention that python3 is simply an alias to python: Homebrew - What's the difference between `python#3` and `python3`?)
I used the following command to create a virtual environment in a folder: python3 -m venv venv. Now within ./venv/bin I have the files in the below screenshot.
I am basically wondering why I have 2 files for Python and 3 files for pip in this virtual environment (the Python files are just aliases, but the pip files are actual executables from what I can tell). From running the version commands (python --version, python3 --version, pip -V, pip3 -V) I can see they are the same version, so I am wondering why I got multiple files.
Is this just for convenience? Does it matter which one I use? And why specifically include a file pip3.7 but not do the same thing for Python? Does this have anything to do with how I'm using Homebrew or is this just a venv thing?
I believe it's just for convenience. On some OSes, like MacOS, where you have both python ( Python 2 ) and python3 ( Python 3 ) having both python and python3 in a venv is nice if you've already written scripts that do something like #!/usr/bin/env python or #!/usr/bin/env python3 as they'll "just work" with the new venv environment. The same holds true for the different pip too.

How can I manage mutiple python in Ubuntu16.04?

  In my Ubuntu16.04, there are python 2 and python 3 default. In addition, i have installed anaconda too. I am sucked by the 'python' cmd. Every time i use pip or pip3 install, I don't know where the package install, python2 or python 3? And I use conda install to install anaconda package. I also use anaconda env to manage different virtual env. But I think it mix with my local Python 2 and 3.
  For example, in directory /usr/bin, I found many soft links like this:
   When i try 'python' cmd, it just confuse me!
   Why python3m are local, shouldn't it be anaconda? Why python3 are anaconda, shouldn't it be local? Then I found that if I use ./python2 or ./python3, I found it is correct now!
  So I know it is caused by environment variables. I echo $PATH, Found it like this: /home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/ant/bin:/snap/bin:/opt/maven/bin:/usr/lib/jvm/java-8-oracle/bin
   I have used update-alternative --config python to configure default python, but it doesn't work! It sames mixed with each others.
   Now I just want to install tensorflow 0.11 in local python3, because in anaconda it is 0.10 version by default. So how can I change this. I just want to use python python3 and python3m represents python2.7 python3.5 and anaconda python respectively, How can I do that! use pip and pip3 for local python2 and python3 respectively!
I ran into a similiar problem when setting up PyCharm Edu to work with Anaconda. I found that I had several versions of Python installed and it was very hard to keep track of which version the IDE was referencing. My CS professor gave me the advice of simply removing the versions of Python I didn't frequent. I now just have Anaconda installed; and use the Anaconda Prompt as my Python console. I also rely on PyCharm's IPython for the developer console. However, if you still want differing versions of Python installed (say your doing QA testing for older devices); there is the really helpful command: which python. When entered into the python console or Anaconda Prompt: which python will display the directory associated with the currently executing Python Shell. This enables you to better keep track of to what particular python.exe the current window is referring to.
Follow up to the comments mentioning using virtualenv and virtualenvwrapper.
Here are the official docs and a good blog post to follow for getting started using virtualenv's is here:
https://virtualenv.pypa.io/en/stable/installation/
http://virtualenvwrapper.readthedocs.io/en/latest/install.html
http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/
Also, once you are setup you can create virtualenv's specifying which python installation you want to use.
which python3
returns
/usr/bin/python3
Then create a virtualenv with that python path. Where example_env is the name of the virtualenv.
mkvirtualenv -p /usr/bin/python3 example_env
Then activate the virtualenv using virtualenvwrapper.
workon example_env
Finally, install tensorflow and other dependencies with pip.
pip install tensorflow
the which command is very useful for finding the path to the executable that is first in your path. Zsh also has the where command, which will show you all instances of the given executable that show up in your path. For managing different python versions, you have a lot of options. The easiest for most people tends to be anaconda, using conda environments. The installer will ask you to add some stuff to your .bashrc file, which will then make anaconda's binaries come first in your path. Anything else you run after the .bashrc gets sourced after that, will then use that first, including PyCharm. For graphical desktop apps to pick up the change, you may need to log out and back in again. If you only need one version each of python 2 and python 3, you can just use the ones available via apt. Depending on your Ubuntu version, Python 2 is definitely installed by default as it is used by many system utilities, including apt itself. Some newer versions may also install python 3 by default, but I do not remember for sure. Another option is to install the versions of python you need in an alternate location, such as /opt/python/<version> and then using environment-modules (installed via apt install environment-modules) or Lmod to control which versions are being used, but that may or may not be easy/convenient to use with a desktop application such as PyCharm.
for TensorFlow, 1.11 is available in anaconda, but I don't remember if it's in the default channel or not.

Confused about Python installations, module installation, and interpreter

So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.

Help with Python structure in *nixes

I came from a Windows background whern it comes to development environments. I'm used to run .exe's from everything I need to run and just forget.
I usually code in php, javascript, css, html and python.
Now, I have to use Linux at my work, in a non changeable Ubuntu 8.04, with permissions to upgrade my system using company's repositories only.
I need to install Python 2.4.3 to start coding in an old legacy system. I had Python 2.5. I downloaded Python 2.4.3 tarballs, ran ./configure make and such. Everything worked out, but now the "default" installation is my system is Python2.4 instead of of Python2.5.
I want help from you to change it back, and if possible, some material to read about symlinks, multiple Python installations, virtualenvs and such: everything I need to know before installing/upgrading Python modules. I installed for example the ElementTree package and don't even know in which Python installation it was installed.
Thanks in advance!
You may have installed Python 2.4 in /usr/local/bin, which, in turn, may come in your $PATH before /usr/bin where 2.5 lives. There are various possible remediations, if that is the case: simplest is probably to rm the link named /usr/local/bin/python (leaving only the "system" one named /usr/bin/python). You will then have to use explicitly python2.4 to invoke the 2.4 installation, while just python will go to the system-installed Python 2.5 installation.
If you have root access you could just create a new simlink.
sudo mv /usr/bin/python /usr/bin/python2.4
sudo ln -s /usr/bin/python25 /usr/bin/python
I don't have too much experience with ubuntu, but i guess it shouldn't brake anything.
To learn more about ln read man ln.
For which version of Python will run when you invoke the python command you will have to manually change the symlink that /usr/bin/python points to, but that won't change what the packaging system considers the "default version of Python" and means you will still have to install version-specific libraries if they are different for a specific version. Luckily, those packages have an easy naming convention, instead of just python-<foo> they are python2.4-<foo> and installing those will put them in the right path (specifically the right site-packages directory).
EDIT: apparently python isn't managed by the alternatives system, silly Debian/Ubuntu
Running
sudo apt-get install --reinstall python-minimal python python2.5
should restore the default Python installation.
Unlike Windows Ubuntu comes with quite a lot of software packaged by the distributor, and it is a good idea to stay with this packages if possible instead of downloading software from the net. Ubuntu 8.04 has Python 2.4.5 (package python2.4), maybe that works for you.
If you need to install Python from source use
./configure --prefix=/usr/local/
instead of a plain ./configure. This makes python to be install at /usr/local/ so it doesn't overwrite the distribution's files
Piggybacking off of #rebus:
sudo ln -s /usr/bin/python2.5 /usr/bin/python
Seems to have worked.

What's the best way to set up symbolic links to current installs, e.g python -> python2.6

What's the best way to set up symbolic links to current installs, e.g python -> python2.6?
I've just installed python2.6 through Macports at /opt/local/bin/python2.6, I'd now like to set up a symbolic link called python here /usr/local/bin/. I then want to be able to add this line at the beginning of my pythons scripts so it knows where to look: #!/usr/local/bin/python. But what happens when I upgrade python to python2.7 for example, do I just need to remember to go to my symbolic link and change it? I guess I'll remember because it likely won't work anymore? Is there a better way to do this?
By default, MacPorts deliberately and carefully installs everything into a separate directory space: /opt/local. This ensures it does not conflict with anything installed as part of OS X or third-parties. To ensure that MacPorts-installed executables are found first, the recommended solution is to modify your shell PATH to put /opt/local/bin before /usr/bin.
MacPorts also provides a special port package, python_select, to manage which python version is pointed to by the command python in /opt/local/bin.
sudo port install python_select
sudo python_select
Then, to make your scripts use your current preferred python, the traditional solution is to use the env program in the shebang line of your scripts.
#!/usr/bin/env python
Symlink the version you use most.
When you need another version, run it by specifying the version number, e.g.:
$ python2.5 dev_appserver.py myapp
Not sure about OSX, here is what I do on Ubuntu 9.04:
>which python
#/usr/bin/python
Just replace that file with a sym link to the version of Python you actually want to use:
>sudo ln -s /usr/bin/python2.6/python /usr/bin/python

Categories