Help with Python structure in *nixes - python

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.

Related

Installed Python 3.8 on Ubuntu, but it's not the default

I have downloaded Python 3.8 on my Ubuntu 16.04 and if i write python3.8 it shows it is present, but when I write python --version I am getting my old Python version which was 3.5
Set an alias:
alias python=python3.8
Then running python in Bash will run python3.8.
To make the change permanent, put the alias line in ~/.bashrc. It will take effect when you open Bash.
Symlinks
Why not symlink /usr/bin/python to python3.8?
Ubuntu follows PEP 394 which says the python command should point to Python 2. However an alias works since it only affects your shell. It doesn't even affect scripts you write, meaning if you want a script to run in Python 3.8, you'll have to write it explicitly in the shebang, e.g. #!/usr/bin/env python3.8 instead of #!/usr/bin/env python.
Why not at least symlink /usr/bin/python3 to python3.8?
Some things will break since there are version-specific libraries. For example _gi is not available for Python 3.8 on Ubuntu 16.04, so Gnome Terminal will not open. See Gnome terminal will not start on Ask Ubuntu for an example.
More details
How to make 'python' program command execute Python 3? on Ask Ubuntu
Everything depends on how you installed the python3.8.
Many methods wouldn't update default symlinks for you.
If you do something like:
sudo ln -s /usr/bin/python3.8 /usr/local/bin/python and run python --version afterward it should solve your issue.
If you python3.8 binary is not in /usr/bin/python3.8 update your symlink path accordingly.
Keep in mind that some apps dependent on specific features of the lower python version might not work correctly. With Python3 the probability is low though.
The correct way is sudo apt install python-is-python3 - it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.
credit to https://askubuntu.com/a/1272899/1055134

Switch between python 2.7 and python 3.5 on Mac OS X

I generally use Python 2.7 but recently installed Python 3.5 using Miniconda on Mac OS X. Different libraries have been installed for these two versions of python. Now, the entering either of the keywords 'python' or 'python3' in terminal invokes python 3.5, and 'python2' returns '-bash: python2: command not found'. How can I now invoke them specifically using aliases 'python2' and 'python3' respectively?
I am currently using OS X El Capitan.
IMHO, the best way to use two different Python versions on macOS is via homebrew. After installing homebrew on macOS, run the commands below on your terminal.
brew install python#2
brew install python
Now you can run Python 2.7 by invoking python2 or Python 3 by invoking python3. In addition to this, you can use virtualenv or pyenv to manage different versions of python environments.
I have never personally used miniconda but from the documentation, it looks like it is similar to using pip and virtualenv in combination.
OSX's Python binary (version 2) is located at /usr/bin/python
if you use which python it will tell you where the python command is being resolved to. Typically, what happens is third parties redefine things in /usr/local/bin (which takes precedence, by default over /usr/bin). To fix, you can either run /usr/bin/python directly to use 2.x or find the errant redefinition (probably in /usr/local/bin or somewhere else in your PATH)
I already had python3 installed(via miniconda3) and needed to install python2 alongside in that case brew install python won't install python2, so you would need
brew install python#2 .
Now alias python2 refers to python2.x from /usr/bin/python
and alias python3 refers to python3.x from /Users/ishandutta2007/miniconda3/bin/python
and alias python refers to python3 by default.
Now to use python as alias for python2, I added the following to .bashrc file
alias python='/usr/bin/python'.
To go back to python3 as default just remove this line when required.
Similar to John Wilkey's answer I would run python2 by finding which python, something like using /usr/bin/python and then creating an alias in .bash_profile:
alias python2="/usr/bin/python"
I can now run python3 by calling python and python2 by calling python2.
Here is how to set the Python version back to 2.7 if you have installed Anaconda3 (Python 3.6) on MacOS High Sierra 10.13.5.
Edit the .bash_profile file in your home directory.
vi $HOME/.bash_profile
Comment out the line with anaconda3 addition to your PATH like this:
# export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
Close the shell, then open it again
You should now see 2.7 when you run python
Then, if you want 3.6 you can simply uncomment out the anaconda3 line in your .bash_profile.
Trying to unlink python will end in tears in Mac OS X.
You will get something like this
unlink: /usr/bin/python: Operation not permitted
I just follow up the answer from #John Wilkey.
My alias python used to represent python2.7 (located in /usr/bin).
However the default python_path is now preceded by /usr/local/bin for python3; hence when typing python, I didn't get either the python version.
I tried make a link in /usr/local/bin for python2:
ln -s /usr/bin/python /usr/local/bin/
It works when calling python for python2.
If you want to use Apple’s system install of Python 2.7, be aware that it doesn’t quite follow the naming standards laid out in PEP 394.
In particular, it includes the optional symlinks with suffix 2.7 that you’re told not to rely on, and does not include the recommended symlinks with suffix 2 that you’re told you should rely on.
If you want to fix this, while sticking with Apple’s Python, you can create your own symlinks:
$ cd <somewhere writable and in your PATH>
$ ln -s /usr/bin/python python2
Or aliases in your bash config:
alias python2 python2.7
And you can do likewise for Apple’s 2to3, easy_install, etc. if you need them.
You shouldn’t try to put these symlinks into /usr/bin, and definitely don’t try to rename what’s already there, or to change the distutils setup to something more PEP-compliant. Those files are all part of the OS, and can be used by other parts of the OS, and your changes can be overwritten on even a minor update from 10.13.5 to 10.13.6 or something, so leave them alone and work around them as described above.
Alternatively, you could:
Just use python2.7 instead of python2 on the command line and in your shbangs and so on.
Use virtual environments or conda environments. The global python, python3, python2, etc. don’t matter when you’re always using the activated environment’s local python.
Stop using Apple’s 2.7 and instead install a whole other 2.7 alongside it, as most of the other answers suggest. (I don’t know why so many of them are also suggesting that you install a second 3.6. That’s just going to add even more confusion, for no benefit.)

How do I re-install python on the mac, but version 3.2 (latest) and have all the command line stuff work

I uninstalled python using the following commands:
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /Applications/Python *
then deleted all python related things in here:
/usr/local/bin
And I just installed python 3.2, but the terminal python doesn't work, $> python doesn't do anything, other than say command not found
I uninstalled all the python stuff, because, there was a problem with the older pythons using global site-packages... so I figured it would be good to start clean (i had like... 5 versions before)
Hmm not sure I'd really go around deleting things that were installed my Mac OS.... grab your install CD and get the default Python back :)
Then install MacPorts type sudo port install python32 and you'll have a seperate install of Python 3.2 in /opt somewhere safe from the default one.
Try ActivePython. Not only does it automatically create symlinks in /usr/local/bin, but also comes with a tool called pythonselect which you can use to set the "active" Python version.
First thing you need to figure out is where your current installation is. The reason I ask is because I have several versions of python installed side-by-side though each with a different name. /usr/local/bin/python is just a sym-link to the real binary file.
For example, maybe you have /usr/local/bin/python-3.2 instead of /usr/local/bin/python. Once you know where it is, all you have to do is create a symbolic link to it via:
ln -s /usr/local/bin/python-3.2 /usr/local/bin/python
That's only an example of course and you're paths might be different. Also you'll have to run that command as root (sudo).

Python - Virtualenv , python 3?

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.

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