It seems that python can be found in three different places in my Mac OS. See below. Is there anything wrong? Should I and how can I clean up my python installation without reinstalling the OS? In fact, I recently experience some strange behavior when using Python.
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/,
'/usr/local/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages',
What you need to do is update the paths in your .bashrc (or more likely .profile as you are on mac). This should be accessible from your home directory. ~/.profile and can be edited using nano.
You can then tell your terminal which set of libraries and version of python to use by adding the following. Note more than one may be added this way, so only have one bin directory with an executable python program!
export PATH=$PATH:/usr/local/lib/python2.7/site-packages
If you want to add other libraries / execute your own programs as if they were in the library or save yourself reinstalling everything, you can use the following:
export PYTHONPATH=/Library/Python/2.7/site-packages'
Finally if you wanted to run a script/preload libraries every time you open python, you can make a .pythonstartup file in your home directory as well.
export PYTHONSTARTUP=$HOME/.pythonstartup
As for cleaning up, most distributions tend to update you paths when they are installed, which is most likely what is causing your problems. So all you probably have to do is look at your .profile file and remove two of the three paths above.
Hope that helps!
Related
I have windows 10 with 2 user accounts, one is an old one that has Python installed. My latest account also has Python installed. I used pip install tweepy, and it now exists in the site packages. If I open my Python shell (v3.6.5), it imports fine. However, I can't get this to work when switching to Python in cmd. In here, after typing python, it says Python 3.6.6. Obviously they are different, yet they seem to be originating from the same directory.
Any ideas?
[edit] I have just realised that I have an external application that runs on Python 3.6.6., so I guess it's defaulting because of that. However, I can't uninstall that since I need it - is there a way to specify which version of Python is launched when I type python into the cmd?
[edit2]: This is the issue, and the solution there works. However, I want to be able to just type python into cmd, rather than the entire path. Since both exist in the path (and I don't want to remove the other 3.6.6.), is there a way to achieve this?
Have you thought about using a virtual environment?
https://virtualenv.pypa.io/en/latest/
When you enter "python" into your cmd it searched python.exe in the directories listed on your PATH variable.
All you need to do is to modify your PATH and add the directory path in which the python with the desired version is located.
Recently, I installed miniconda, and because of which, my default python directory seems to have changed to,
/home/user/miniconda3/bin/python
Also, the default version seems to have changed to python 3.7 instead of python 2.7
And I cannot seem to install various libraries like tensorflow.
How do I get my python back to normal?
The miniconda installation has properly added itself to your PATH. You can check whether /home/user/miniconda3/bin is listed when you run
echo $PATH
in a terminal. If so, it most likely added some lines to the bottom of your ~/.bashrc file. Simply remove these lines yourself, and your PATH should be back to normal (save the file and open up a new terminal), meaning that typing e.g. python will give you the system Python.
I have downloaded a python package to install, on my ubuntu machine. The package has already a setup.py file to use, but I want to change the default python installation address to something else, for this package specifically (and not for good). So what I tried is:
First in the terminal, I export that address of the new folder:
export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages
Then I add this exported address as prefix to the installation command:
python setup.py install --prefix=~/Documents/testfolder
The installation goes through. Now to make python always look for this new path as well (next to the default installation path), I export the address in bashrc file:
export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"
But now whenever I open a terminal and try to import the installed package, it cannot see ("no module named..."). Only when I open a terminal in the folder where I had the installation files (namely setup.py), and run python, can it then see the package, and it works there.
Why isn't my export in bashrc making the package available from anywhere?
Is there something I have done wrong in the above?
To answer your question about the export path. Do you have $PYTHONPATH as a part of your $PATH? If not you should add it to path.
The best way to handle this scenario in my opinion is to use a virtual python environment. There are a couple to choose from, but I like virtualenv the best. The reason to take this approach is because you can manage different versions of python in separate folders. And have separate packages installed in these folders. I recommend looking into it as it is a very useful tool. If you want an examole of how to use it i can provide that https://virtualenv.pypa.io/en/stable/
I installed python using MacPort and I am satisfied with the result, but found that there are other versions of Python installed in other directories, and can not remember how they were instaldas, it's been five years that use this notebook and perhaps installed by other means a few years.
I tried to remove all references to extra Python, beyond that were installed with MacPorts, but do not think like, I tried to remove the directories with the command rm -rfmas even using sudo rm -rf have success.
The old instalation are in directories:
/System/Library/Frameworks/Python.framework/Versions/
/Library/Python/
How to discover the origin of such facilities and remove permanently?
Don't remove the system Pythons. They may be used by other programs. (I don't know if anything on OS X actually uses them, but it's best to keep them.)
Instead, just make sure that your MacPorts bin directory (at /opt/local/bin) is first on your $PATH.
Don't! The name /Library and /System suggest that they are OS-level directories. Nobody installed them. Instead, Mac and other linux-based systems use them by default for system-level services (and they should not be even manually upgraded or system stability may suffer).
For all what matters, you should just prepend your installation directory to a system variable called PATH in your $HOME/.bashrc file. Then, whenever YOU use python, the system will always search for the first occurrence of python on PATH, which is your python. Open terminal, enter the following command (once in a life time):
echo "PATH={a-path-to-the-folder-containing-your-executable-python}:\$PATH" >> $HOME/.bashrc
To explain it, the quoted command prepends your installation directory as the first place to search for executable files. The >> $HOME/.bashrc write this command to the last line of .bashrc, which is a file that setup your terminal environment automatically upon login.
I need scipy on cygwin, so I figured the quickest way to make it work would have been installing enthought python. However, I then realized I have to make cygwin aware of enthought before I can use it, e.g. so that calling Python from the cygwin shell I get the enthought python (with scipy) rather than the cygwin one.
How do I do that?
I can guess my question is easy, but I'm just learning about all of this and so please be patient :-)
Think about uninstalling the cygwin version of Python, at least to start with. It's easy enough to reinstall later.
Copy your Windows path.
Install the Windows version of Enthought Python.
Examine the new Windows path. The new entries probably have to be transferred to your bash shell in cygwin. (I haven't seen Enthought. There might be more than one new path entry.) But test without changing the bash path, just to be sure.
Add the new path entries to, umm, .bashrc, I think. The path under cygwin will be a colon delimited string. You'll need to use the /cygdrive path; expect to use an entry like this to put Entought in the path.
/cygdrive/c/Program Files/Enthought
To actually do that, edit .bashrc, and put these two lines (or something like them) at the end.
PATH=$PATH:/cygdrive/c/Program\ Files/Enthought
export PATH
Note that the backslash allows proper interpretation of spaces in file paths. The export statement guarantees that programs called by bash will also include that path.
Just put the directory with enthought python before the directory with Cygwin's python in your path. If both are in the same directory, use ln to create a symbolic link, store it in another directory, and place it higher in your path. The previous answer has instructions to add it to your path.