Python will not recognize MacPorts installed packages - python

Thank you in advance for the advice!
Background:
Relative to the people on this site I'm new to programming, and despite some research I'm not familiar with using Unix-type shells, with what managing packages really involves, or with anything which is done outside of the "Dr. Java" IDE or default R GUI. I have a late-2008 macbook with OSX 10.5.8
My Goal:
I'm trying to get the following packages installed in Python: scipy, numpy, matplotlib, networkX
My Story:
From what I understand my 10.5.8 comes installed with a Python 2.5, which the system needs for its functioning, and you really shouldn't mess with. The packages I installed require Python 2.7, so after reformatting my machine, I installed Python 2.7.2 for OSX 10.3 (the latest Python/OSX that was offered of 2.7.X) using the .dmg off of the Python website. Then, I installed XCode using the .dmg (appropriate version for my OS) off of the Apple site, and installed MacPorts (appropriate version for my OS) using the .dmg off of their site. Then, I believe I used MacPorts to install the packages:
sudo port install py27.numpy
However, when I try to import the packages I've apparently installed, Python tells me they don't exist. I'm pretty sure I have only 2 versions of Python on my machine, 2.5 for OSX and 2.7.2 for me (how do I check this?), and if I check which version of Python is running, whether through the Shell or through the IDLE, I get:
>>> import sys
>>> print sys.version
2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39)
[GCC 4.0.1 (Apple Inc. build 5493)]
If I run "which python" i get:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
When I check where MacPorts installed the packages using
port contents py27-packagename
I get directories mostly in:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
with a few directories in
/opt/local/share/py27-matplotlib/examples/
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/share/doc/networkx-1.5/
and one directory each in
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/f2py
/opt/local/share/doc/py27-matplotlib/CHANGELOG
/opt/local/share/doc/py27-matplotlib/README.txt
/opt/local/share/doc/py27-matplotlib/TODO
My Question:
How do I get Python to recognize my packages?? Ideally I'd like to set it up so I can use the Python IDLE, as well as invoking it from the command line.
I've tried replicating some of the solutions at:
Locate MacPorts package?
But I don't really understand what I'm doing all that well so It's difficult to adapt what's being done there.
Thank you again everyone!

i initially misunderstood your question to be the standard one of - why can't i run my macports python? but in fact, you specified that you are trying to use the self-installed python, and you are. the problem is, that python doesn't know where to look for your libraries which macports installed.
you were on the right track with that question you linked to. see this response, but replace the path with your actual path to your macports libs, viz:
export PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH"
see if that doesn't do the trick!

Related

What are the supported versions of Python for Visual Studio Code?

Or where is there a published list of the supported versions of Python for Visual Studio Code?
Also if one's machine's OS makes a difference that would be helpful to know as well.
According to their documentation, if you link to what "a supported Python", is, the requirements are "whichever version of Python you want to use". And they explicitly include:
The built-in Python installation on Linux.
An installation through Homebrew on macOS using brew install python3 (the system install of Python on macOS is not supported).
A download from python.org.
A download from Anaconda (for data science purposes).
Of course python.org has versions all the way back to 0.9.1, although I suspect anything older than the 2.0.1 in the main downloads doesn't really count.
More realistically, they probably only test with current Python versions, which as of right now means 2.7, 3.5, 3.6, and maybe 3.7 prereleases. If you wanted to use 2.5 or 3.3, you might well get syntax highlighting and error fly-checking based on 2.7 or 3.5 grammar, etc. But actually running your code should still work.
Also, if you're on a Mac and thinking of Homebrew, it's worth noting that brew install python3 will give you an error; the package is just called python now, and you install python for 3.x and python#2 for 2.x.
Microsoft's Python Extension for VS Code actually let you select an interpreter from a list of detected or manually set environments (CMD-Shift-P/Ctrl-Shift-P -> Python: Select Interpreter), so it should work with latest version of any interpreter, as long as is supported by the OS (remember that VS Code and its extensions are hosted in Electron). Source

How do I get python to import pandas?

I installed Python 3.5.1 from www.python.org. Everything works great. Except that you can't install pandas using pip (it needs visualstudio to compile, which I don't have). So I installed Anaconda (www.continuum.io/downloads). Now I can see pandas as part of the list of installed modules, but when I run python programs I still get:
ImportError: No module named 'pandas'
How do I set up my environment to use the modules from Anaconda?
Note: I have Anaconda's home directory and Library/bin on my path, as well as Python's home directory. I do not have PYTHONPATH or PYTHONHOME set, and I know I have the correct privileges to see everything.
I have successfully installed pandas for a Windows 32 bit version Python 3.4 with pre-complied code (no Visual Studio required) using the website:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyyaml
There is link for Python 3.5 pre-complied code at this site as well, but I have not tested it.
Download the code you want to a directory on your machine.
Using your Windows CMD.exe, go to your python directory and enter:
Python -w pip install "YourDirectory/pandas-0.18.1-cp35-cp35m-win32.whl"
OR
Python -w pip install "YourDirectory/pandas-0.18.1-cp35-cp35m-win_amd64.whl
Choose the version based on the version of Python you have
have, 32 bit or 64 bit.
Good Luck!
Anaconda has included one version of Python with it. You have to change your system environment path with Anaconda's instead of the former one to avoid conflict. Also, if you want to make the whole process easy, it is recommended to use PyCharm, and it will ask you to choose the Python interpreter you want.

How do I change the kernel/python version for iPython?

I have installed iPython using pip in OS X 10.10, and it gave me the "ipython" and "ipython2" commands, which run great, but which use OS X's default python version 2.7.9. I downloaded and installed the latest release of Python3.4 and can load it with the command "python3," but cannot find a way to get iPython to use this version of python. The iPython Web site states the package can be used with python versions 3.3 and above, but I cannot find any instruction on how to change the default python version used.
So far I have found that the jupyter package for iPython has a kernel specification in /usr/local/share/jupyter/kernels/, which is just a folder called "python2" containing a json file that points to the system's python 2.7.6, but altering this to point to the new python3.4 installation does not work. My guess is this configuration is for the ipython notebook.
I've also tried the approach here: ipython reads wrong python version
In doing so I've duplicated the ipython2 command in /user/local/bin/ and edited it to use the python3 interpreter located at /Library/Frameworks/Python.framework/Versions/3.4/bin/python3, however, this gives me an error "ImportError: No module named 'IPython'," which suggests the python3 installation does not have ipython installed.
To tackle this, I've tried uninstalling ipython and reinstalling it using pip, but it just targets the system's Python 2.7 installation and does nothing for python3.
Does anyone know how to configure iPython to use a different python version, or even install a separate ipython installation for python3? Ultimately it would be nice to quickly switch back and forth depending on my needs.
I just found the answer. In essence, this stems from not understanding the python installation layout and how resources are separated between installed interpreters. It appears each python version will have its own repository of tools, and the current "pip" command I had installed on the system was mapped for use with python 2.7, so all libraries, tools, and other details it managed where available only to python 2.7. This included iPython.
I thought that installing python3 would make all these libraries available to the new interpreter by default, but it appears the system keeps them all separate (which makes sense). The main issue here was continuing to use "pip" which targeted the old installation, instead of the new "pip3" command included with python3. By using pip3 to reinstall iPython, I was able to get iPython3 installed properly.
Unfortunately this setup means needing to re-download other libraries, but that's not too difficult.
It might seem like a trivial issue in hindsight, but this had me completely stuck. I hope this helps someone else in this situation.

Changing default Python 2.x version in XCode 6.1 (Yosemite)

My Mac came with Python 2.6.9 and 2.7.6 (the default 'Python') installed. I needed several modules from the SciPy stack and installed them using MacPorts:
py27-numpy
py27-scipy
py27-matplotlib
py27-pandas
Now for whatever reason this installed another Python version alongside the ones I already had, Python 2.7.8. All the packages only work with this one. When I start a new project in XCode and point it to this new instance, /usr/bin/python2.7, nothing works and I get
'/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '
I can make XCode work with the 2.7.6 version, /usr/bin/python, but then none of the packages I need are available.
Is there any way to either make XCode work with this other python instance, or to remove the 2.7.6 entirely and replace the default python with the new one?
The correct path to the MacPorts python is /opt/local/bin/python2.7. By using the MacPorts version of Python, you are ensured that all of the necessary dependencies are found. I was able to follow these instructions and get everything to work. The only catch was in step 16 I had to manually enter the path because /opt was not visible.
Alternatively, you could try adding the MacPorts library path to your PYTHONPATH. Just add
export PYTHONPATH=$PYTHONPATH:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
to your .bash_profile. I will warn you that when I tried mixing Pythons in the past I frequently got errors relating to trying to import wrong module.

Macports claims python is 2.7.6, however python --version says 2.7.5

I am on osx-mavericks and I am encountering a Segmentation fault: 11 issue.
This SO post: Segmentation fault: 11 in OS X
says that this could be caused by python 2.7.5
I attempted to upgrade python to the next version.
When I do
port installed | grep python
I see:
python27 #2.7.6_0 (active)
However, when I run
python --version
I get
Python 2.7.5
It does the same behavior for python2.7 --version as well. Can anyone help me with this? How do I get python up to 2.7.6? Is it already there and just acting weird? Do I still have 2.7.5 on the machine somewhere? (Also, its worth noting I'm a bit new to the osx development environment)
Thanks to all.
I found the macports python at /opt/local/bin/python, whereas which python showed me /usr/local/bin/python. I changed the order in which these two directories are seen in my PATH and everything seems to work now.
OSX comes with python. It is likely that the version of python on your path is the out of box version. If you use
which python
you should get the directory location of the python you are calling and you can see if it is the macports or OSX version. The OSX Python on my 10.8 mac is in /Library/Frameworks/Python.framework .
Using Ned Deily's comment below you can find the location of the python you want to use from the terminal. Then you can add a line to your ~./bash_profile to set the PYTHONPATH to the version you want to use like this.
export PYTHONPATH=$PYTHONPATH:/path/to/your/python
Then you must reopen the terminal or source the .bash_profile.
source .bash_profile
Then use the which command again to make sure you have the version you want.

Categories