Howto symlink Python to Python2.7 as non-superuser - python

I am a non-superuser of a linux machine.
Currently it has 2 versions of Python.
When I invoke standard python command it gave version 2.6
$ python
[neversaint#mach71 ~]$ python
Python 2.6.2 (r262:71600, Jan 28 2011, 13:47:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which python
/opt/somedir/bin/python
It's only when I invoke with python2.7 it gives the version 2.7
[neversaint#mach71 ~]$ python2.7
Python 2.7.6 (default, Nov 11 2013, 13:13:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which phython2.7
/usr/bin/python2.7
My question is how can I set it such that whenever I call $ python it will give me version 2.7.

You can simlink it into some directory both accessible to your user and in your $PATH. For example if /home/<your-username>/local/bin is in your $PATH then you can do
ln -s /usr/bin/python2.7 /home/<your-username>/local/bin/python
In this example /home/<your-username>/local/bin should be in your path before /usr/bin. If there is no such entry in your $PATH you can add it there:
export PATH=$HOME/local/bin:$PATH
You can also add this line to .bashrc or similar to activate it on shell start.

Use a shell alias, alias python=/usr/bin/python2.7 and then python will execute the result of that alias.

in /usr/bin create symlink to python27 or whatever python version you have
sudo ln -s python2.7 python

Related

Move from python 2 to python 3 on Mac

I have both python 2.7.16 and python 3.7.3 on my Macbook air.
I don't use python 2.7.16 so I want to remove it, but I understood that this could break my Mac.
I am frustrated from using python3 and pip3 instead of python and pip is there a way to make all of the python3 commands to be accessed by using python (without 3) instead of python 2 and make python 2 be accessible by using python2?
tnx ahead
(base) shrub$ /usr/bin/py
pydoc python python2.7 pythonw
pydoc2.7 python-config python2.7-config pythonw2.7
Looking in /usr/bin there is a python executable (which when run opens a shell with python3) and a python2.7 executable (which when run opens a shell with python2).
(base) shrub$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
(base) shrub$ python2.7
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Running python scripts with python (python hello.py for example) should default to python3 and running scripts with python2.7 (python2.7 hello.py for example) will run them with python2.
Also there is a script called 2to3 which can help you with converting your python2 code to python3 :)
https://docs.python.org/3.0/library/2to3.html

In Python from Cygwin, os.symlink not creating proper Symlink

Based on below code, I am trying to create symlink from Cygwin command prompt. It generates a symlink file but it seems not valid windows symlink.
Below code works fine and generates proper symlink if I run the same code from native command prompt.
$ python3
Python 3.6.8 (default, Feb 14 2019, 22:09:48)
[GCC 7.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.symlink("package.jpg", "sample.jpg")
>>>

LD_LIBRARY_PATH with sudo python

I'm trying to use a newer Python (2.7.3) with an old CentOS.
I have a recipe to install python to a non-standard location:
./configure --prefix=#{install_path} --with-threads --enable-shared --with-zlib=/usr/include
make
make install
I set the PATH and LD_LIBRARY_PATH variables to find bin/python and the .so files using /etc/profile.d/. This seems to mostly work.
With a non-root user, I get the right Python:
[vagrant#localhost ~]$ python
Python 2.7.3 (default, Dec 24 2012, 15:18:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
With a root user, I get the right Python:
[vagrant#localhost ~]$ sudo su
[root#localhost vagrant]# python
Python 2.7.3 (default, Dec 24 2012, 15:18:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
However, the $LD_LIBRARY_PATH hack seems to be a little wonked when using sudo:
[vagrant#localhost ~]$ sudo python
python: error while loading shared libraries: libpython2.7.so.1.0:
cannot open shared object file: No such file or directory
Even though the variables look right:
[vagrant#localhost ~]$ sudo which python
/opt/Python-2.7.3/bin/python
Adding Defaults env_keep += "LD_LIBRARY_PATH" to /etc/sudoers does not work.
sudo -i python does work.
sudo -E python does not work.
I'm curious what I could do to get sudo to pick up the right Python without -i?
related:
sudo changes PATH - why?
https://stackoverflow.com/questions/12593336/odd-path-behaviour-on-centos-python
Thanks to this blog post. You can forego the use of $LD_LIBRARY_PATH by linking with LDFLAGS in configure. Where #{ldlibpath} is #{install_path}/lib:
./configure --prefix=#{install_path} --with-threads --enable-shared \
--with-zlib=/usr/include LDFLAGS="-Wl,-rpath #{ldlibpath}"
As noted in the blog post, you will need to mkdir this ldlibpath before running configure.

Installing SetupTools on VPS Running CentOS 5.4

I am trying to install setuptools via an egg on my VPS. However, I keep getting an error that Python2.6 doesn't exist despite the fact that it is in $PATH:
[root#host install]# sudo sh setuptools-0.6c11-py2.6.egg
setuptools-0.6c11-py2.6.egg: line 3: exec: python2.6: not found
[root#host install]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/courier-imap/sbin:/usr/lib/courier-imap/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/wt_python/bin
[root#host install]# sudo /usr/local/wt_python/bin/python2.6
Python 2.6.5 (r265:79063, May 18 2010, 16:49:22)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root#host install]# sudo python2.6
Python 2.6.5 (r265:79063, May 18 2010, 16:49:22)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Any idea what could be wrong?
Thanks
1 ) sudo as root is redundant.
2 ) for a quick hack symlink your python install into a standardized path. I'm not 100% familiar with the setuptools installer, but it could spawn a subshell that would mess with environment variables. For that matter sudo depending on flags messes with environment variables.
3) If there isnt a reason why you specifically need a custom python build why not use the one in the repositories?
Hope one or a few of the ideas help.
Rob

Adding a shebang causes No such file or directory error when running my python script

I'm trying to run a python script. It works fine when I run it:
python2.5 myscript.py inpt0
The problem starts when I add a shebang:
#!/usr/bin/env python2.5
Result in:
$ myscript.py inpt0
: No such file or directory
Try 2:
#!/usr/local/bin/python2.5
Result in:
$ myscript.py inpt0
: bad interpreter: No such file or directoryon2.5
When I run them directly in the terminal they both work just fine:
$ /usr/local/bin/python2.5
Python 2.5.4 (r254:67916, Feb 9 2009, 12:50:32)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ /usr/bin/env python2.5
Python 2.5.4 (r254:67916, Feb 9 2009, 12:50:32)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Any hints on how to make this work with shebang?
I had similar problems and it turned out to be problem with line-endings. You use windows/linux/mac line endings?
Edit: forgot the script name, but as OP says, it's dos2unix <filename>

Categories