Locate MacPorts package? - python

I just installed the py27-numpy package via MacPorts and python will not find the module when I use this command: import scipy
I used the help('modules') command and the scipy port did not come up.
Clearly the path is not configured correctly or MacPorts is not installing in the correct place, but either way, it would solve my problem to know where this package is being installed.
Where can I find the path to MacPorts-installed package, py27-scipy?
Output of echo $PATH command:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:directory/bin
I cannot find the package in any of those locations.
Thanks for the help.

To find the location of installed components, use the contents subcommand:
port contents py27-numpy
As for getting python to find the package, see #fardjad's response.

Your PATH is incorrect. It appears to be picking up another Python 2.7, likely one installed using a binary installer from python.org or elsewhere, and not the MacPorts installed one. Try removing the the /Library/Frameworks/Python.framework/Versions/2.7/bin from PATH or just invoke the MacPorts Python directly:
/opt/local/bin/python2.7

MacPorts should install Python packages in /opt/local/Library/Frameworks/Python.framework/2.7/site-packages by default. So make sure to set $PYTHONPATH environment variable in your .profile file:
export PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/site-packages"

sudo port select --set python python27
is the best answer to install port's python system-wide

With Homebrew only using the latest, the Mac system version, and MacPorts for the others in-between, I was confused until I found python locations differ depending on the installer.
Here's an opinionated tip: Use virtualenvs for your projects and don't change your default version with the MacPorts. I won't and don't want to remember to updoot my python in the middle of something so I rely on virtualenvs. Choose and find the python version on the computer, then mkvirtualenv --python=/found/u/python3.X getawesome.

Based on Jeremy W. Sherman's answer
I checked my python version
python --version
Python 3.8.5
and location:
which python
/opt/local/bin/python
and then tried:
sudo port contents python38
which lists 7285 lines:
Port python38 contains:
/Applications/MacPorts/Python 3.8/IDLE.app/Contents/Info.plist
/Applications/MacPorts/Python 3.8/IDLE.app/Contents/MacOS/IDLE
/Applications/MacPorts/Python 3.8/IDLE.app/Contents/MacOS/Python
/Applications/MacPorts/Python 3.8/IDLE.app/Contents/PkgInfo
...
/opt/local/share/man/man1/python3.8.1
combining that with fardjad's answer leads to:
sudo port contents python38 | grep site-packages
with the output:
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/README.txt
since we need the directory modifying the command to:
dirname $(sudo port contents python38 | grep site-packages)
gives the desired directory:
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
so it's possible to end up with the one-liner:
One-Line PYTHONPATH setting in macports:
export PYTHONPATH=$(dirname $(sudo port contents python38 | grep site-packages))
and we can check the result:
echo $PYTHONPATH
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
For reference see how the Eclipse Liclipse python IDE dialog for setting the PATH looks - there are some more directories you might want to include for a fully specified PYTHONPATH.

Related

Why is Python Installation in "Library" directory macOS?

I'm on a new Mac and installing Python. I believe I reinstalled my Python using Homebrew. The path, however, is unusual to me, and I'm wondering why it is located here:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
I'm used to Python being somewhere like this, instead (which is also is):
/Users/user.name/Library/Python/3.9/bin
What is different about the first installation? Are there any special considerations if my Python is located here?
/Library/Frameworks/Python.framework/Versions/3.x/bin/python3 is the Python distribution shipped with macOS.
It's usually a good practice to create your own Python environment for your projects instead of using the default Python installation (consider reading about pyenv).
If you want to use your Homebrew installation, run brew info python to find your Python installation path, then append the parent directory's path to your PATH variable by editing ~/.zshrc (assuming you're using zsh, the default shell).
$ brew info python
....
Python has been installed as
/opt/homebrew/bin/python3
...
# inside ~/.zshrc
...
export PATH="/opt/homebrew/bin:$PATH"
....

Homebrew macOS - Python installation

I've installed python via homebrew. It is located in:
/usr/local/Cellar/python/2.7.13_1
which should be right.
Now I am trying to use this python installation, but "which python" only shows the macOS python installation at "/usr/bin/python". So i am checking the $PATH and I see that everything should be ok.
"echo $PATH" results in this: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I restarted the terminal window and this occurs every time. I also did the
"brew doctor" and no warnings appeared.
What I am using:
Standard macOS Terminal-App
Has anybody a clue how this problem could be solved?
Update $PATH in your .bashrc file.
Example add the following line in your .bashrc
export PATH=/usr/local/Cellar/python/2.7.13_1/bin:$PATH
Either add the Python Homebrew prefix to your $PATH, like #Zico suggests in his answer, or link the Python executable into /usr/local/bin (which might already be in your path)
You'd do that with
$ brew link python
Good luck :)
Brew create an python2 alias to
/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I just created a copy of python2 alias and rename it to python. That solved the problem

How install and use another version python(python 2.7) on linux with the default python version is python 2.6

There is a default python version, namely python 2.6, on the GPU server with Linux OS. Now I want to install a new python version on the server from its source, namely python 2.7. I should not change the default python version since I am not the administrator and some reason. So what should I do?
You can install your new version of Python. It should be accessible with the python27 command (which may be a symbolic link).
Then you will just have to check that the python symbolic link still points to python26.
Doing this, python will keep on execute Python 2.6 while python27 will execute Python 2.7
You can use virtualenv, to execute your programm in an environment with python 2.7.
Install virtualenv and virtualenvwrapper (for comfotable use.)
mkvirtualenv -p <your-python-version> would then start a virtual environment where the desired python version is the default.
To build on Tryph's answer, you can install that new version to your home directory, then in a directory specified within your PATH (like in .bash_profile), you can point to that directory and within there create a sym-link that points to the new python.
For instance, if you have a bin folder in your home directory that is specified in the path
ln -s /bin/python ~/bin/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