How to set defualt python library path - python

I'm using FreeBSD 7.2. I upgraded to Python 2.6. However when I run any python app, it is still using /usr/local/lib/pytho25 as the library path. How do I change it? I cannot modify the python app. Basically I need to change the default lib path to python26.

Are you sure you're using the new binary? I'm not familiar with FreeBSD, but with OpenBSD you have to do a ln -s /usr/local/bin/python2.6 /usr/local/bin/python if you want to start it as python. Perhaps it still points the old way? Also there is PYTHONPATH

You can creat a softlink to your desiered python executable:
$ cd /usr/bin
$ ln -s python2.6 python
This way python command points to python2.6

You have probably not migrated correctly to python 2.6 as you should no longer have python 2.5
You can follow the UPDATING notes here to completely get rid of python2.5 (this is for python2.7 but you can safely follow those instructions):
http://www.freshports.org/lang/python27/

Related

change python version in terminal and intelliJ

short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.

Always run python 2.6 instead of 3.4 in centos

I have installed python 3.4 in centos.
So I opened terminal and typed python or idle then always ran python 2.6 not python 3.4.
How can i run python 3.4 instead of 2.6?
thanks you.
Do not replace the default Python! CentOS's system tools such as yum, system-config-* tools and several other things rely on the default Python 2.6 installation. Set up a virtual environment instead, where you can define which is the default version.
virtualenv --python=/usr/bin/python3.4 myenviron
source myenviron/bin/activate
First, look for where your Python 3.4 is located:
$ which python3.4
/usr/bin/python3.4
See if ~/bin (e.g., /home/username/bin) directory is in the PATH environment variable:
$ echo $PATH
/home/username/bin:/usr/local/bin:/usr/bin:/bin
If not, add the bin directory to PATH, ideally within your ~/.bashrc file. Then create a symlink pointing to the Python interpreter under the bin directory:
$ ln -s /usr/bin/python3.4 /home/username/bin/python
This way, when you type python on the command line, the interpreter specified will be launched.
I am on Debian/Wheezy, so the detail might be slightly different, but a similar approach should work.

Python set to /usr/local/bin/python how to set to /usr/bin/python

I made the mistake of installing python 2.7.6 without using home brew and now my python is set to the python in /usr/local/bin/python (which is 2.7.6).
I want to get rid of python 2.7.6 and change my default python to the standard system python 2.7.5 in /usr/bin/python.
How do I do this?
Did you take a look at this? I assume you are using bash shell.So you can put
PYTHONPATH="$PYTHONPATH:/usr/bin/python"
in your ~/.bash_profile. For me , I use zsh. So I place it in ~/.zshrc.

How to set Python's default version to 3.x on OS X? [duplicate]

This question already has answers here:
How to change default Python version?
(19 answers)
Closed 1 year ago.
I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.
Currently:
$ python
version 2.7.5
$ python3.3
version 3.3
How do I set it so that every time I run $ python it opens 3.3?
Changing the default python executable's version system-wide could break some applications that depend on python2.
However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put
alias python='python3'
in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:
[ -e ~/.profile ] && . ~/.profile
This way, your alias will work across shells.
With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.
If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:
alias 2='python2'
alias 3='python3'
Tip: For scripts, instead of using a shebang like:
#!/usr/bin/env python
use:
#!/usr/bin/env python3
This way, the system will use python3 for running python executables.
You can solve it by symbolic link.
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
Open ~/.bash_profile file.
vi ~/.bash_profile
Then put the alias as follows:
alias python='python3'
Now save the file and then run the ~/.bash_profile file.
source ~/.bash_profile
Congratulation !!! Now, you can use python3 by typing python.
python --version
Python 3.7.3
I encountered this issue as well, so I thought I should post an updated answer. Please note that this will only apply to a Mac-based setup (I haven't tried it with Windows or any flavor of Linux). The simplest way to get this working is to install Python via Brew. If you don't have brew installed, you will need to do that first. Once installed, do the following in at the terminal:
brew install python
This will install Python 3. After it's installed, run this:
ls -l /usr/local/bin/python*
You will see all of the links created by brew to its Python install. It will look something like this:
lrwxr-xr-x 1 username admin 36 Oct 1 13:35 /usr/local/bin/python3# -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x 1 username admin 43 Oct 1 13:35 /usr/local/bin/python3-config# -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x 1 username admin 38 Oct 1 13:35 /usr/local/bin/python3.7# -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x 1 username admin 45 Oct 1 13:35 /usr/local/bin/python3.7-config# -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x 1 username admin 39 Oct 1 13:35 /usr/local/bin/python3.7m# -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x 1 username admin 46 Oct 1 13:35 /usr/local/bin/python3.7m-config# -> ../Cellar/python/3.7.4_1/bin/python3.7m-config
The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:
ln -s -f /usr/local/bin/python3 /usr/local/bin/python
You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:
exec $SHELL -l
You're all set now. Now, you can do:
which python
and it should show:
/usr/local/bin/python
All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:
python --version
Go to terminal type:
alias python=python3.x
This will setup default python as python3.x
This worked for me. I added alias and restarted my terminal:
alias python=/usr/local/bin/python3
The following worked for me
cd /usr/local/bin
mv python python.old
ln -s python3 python
Go to 'Applications', enter 'Python' folder, there should be a bash script called 'Update Shell Profile.command' or similar. Run that script and it should do it.
Update: It looks like you should not update it: how to change default python version?
I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.
You have to add/modify your commands in ~/.zshrc instead.
$ sudo ln -s -f $(which python3) $(which python)
done.
Mac users just need to run the following code on terminal
brew switch python 3.X.X
3.x.x should be the new python version.
This will update all the system links.
UPDATE
For Newer version of MAC use
brew link python 3.X.X
Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:
pyenv global 3.8.2
and then in any specific project, you can create a .python-version file which has the python version inside of it:
pyenv local 2.7.1
This is the best way to manage multiple versions of python on a system in my opinion.
I think when you install python it puts export path statements into your ~/.bash_profile file. So if you do not intend to use Python 2 anymore you can just remove that statement from there. Alias as stated above is also a great way to do it.
Here is how to remove the reference from ~/.bash_profile
- vim ./.bash_profile
- remove the reference (AKA something like: export PATH="/Users/bla/anaconda:$PATH")
- save and exit
- source ./.bash_profile to save the changes
On MacOS
Step-1: Upgrade python to latest version by:
$ brew upgrade python
Step-2: Go to home:
$ cd
Step-3: open .bash_profile
$ vi .bash_profile
Setting PATH for Python 3.8
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
Step-4: Save the file. And compile it by:
$ . .bash_profile
Step-5: Check the python version:
$ python -V
Step-6: Thats all.
This is the simplest way from my exp. (if you have brew installed on your mac).
Try this from your terminal:
brew install python3
and then run the below on your terminal :
ls -l /usr/local/bin/python*
Tip:
** (note down the python version 3.8 or 3.9 thats displayed on the terminal. This will be required in the next step). for e.g. in my case it was:
lrwxr-xr-x 1 user admin 24 May 7 14:33 /usr/local/bin/python -> /usr/local/bin/python3.9
Now run the below command on your terminal:
ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python
(where 3.9 is the version displayed on your terminal with the previous command)
Its DONE !
To test your default version of python:
close the current terminal or start a new terminal and
run the below command :
python --version
Happy Coding!
I'm not sure if this is available on OS X, but on linux I would make use of the module command. See here.
Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.bashrc):
module load python3.3
This will make it so that your paths get switched around as required when you log in without impacting any system defaults.
For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.
install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3
If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:
How to: Macports select python
TL;DR:
sudo port select --set python python27
If you are using a virtualenvwrapper, you can just locate it using which virtualenvwrapper.sh, then open it using vim or any other editor then change the following
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
Change the line VIRTUALENVWRAPPER_PYTHON="$(command \which python)" to VIRTUALENVWRAPPER_PYTHON="$(command \which python3)".
If you are using macports, that has a easier way to do:
run:
port install python37
after install, set default:
sudo port select --set python python37
sudo port select --set python3 python37
restart your cmd window, finished.
Well... It's kinda old. But still deserves a good answer.
And the good one is You Don't Wanna Touch The Default Python On Mac.
Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it's still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide... whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.
The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency... hope you understand what I mean. A good developer doesn't care. Good developers create clean playgrounds with python version they desire.

How can I use Python 2.6 in Ubuntu 11.10?

The default version of Python on Ubuntu 11.10 is 2.7, but I'm looking for 2.6. How do I make it default and where is the executable located?
I type which python2.6 but it returns nothing, yet I did have a python2.6 folder under /usr/lib/python2.6. But it doesn't look like the python2.7 which is at the same path /usr/lib/. Inside the python2.6, there are two folders: dist-packages and lib-dynload.
Actually I am configuring PyDev, and it requires me to specify where the python2.6 executable is. The python2.7 executable has been easily located by just using auto configuration as it is the default.
You can install the package python2.6 (apt-get install python2.6). At this point, the default version of Python will still be 2.7. You can change this via
ln -s /usr/bin/python2.6 /usr/bin/python
Note that there's a decent chance this could cause problems with your system. Several scripts assume the default version of Python is 2.7 and may break when run under a different version. If you have a script that explicitly requires Python 2.6, you can add a shebang at the beginning of your script to specify the version
#!/usr/bin/python2.6
On many systems, one version of python is the default. The rest get called by their name and version number:
~ $ python --version
Python 2.7.2
~ $ python2.6 --version
Python 2.6.7
Per the release notes, these should be available in Oneiric.
Your other questions:
Where is it? Run $ which python2.6 to find out.
How to make it the default? The safest way is to use alias so that change will be only visible to you. Otherwise, if you repoint /usr/bin/python to an unexpected version of Python, you will may break the OS scripts that rely on Python2.7. Rather than changing the default, it is better to just call the specific version of Python you need.

Categories

Resources