53

I have managed to get spyder installed and functioning on my mac but I want to add in a few modules that it doesn't include by default (mahotas and pymorph).

I installed both via easy_install in the terminal and both seemed to install without any error messages. Running python from a terminal and using import mahotas and import pymorph works just fine without error messages. However, when I run the same lines from a script within spyder I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 493, in runfile
 execfile(filename, namespace)
File "/Users/Name/Documents/Python/dna.py", line 11, in <module>
 import pymorph
ImportError: No module named pymorph

All I'm trying to do right now is run the import lines, that's it. I can't seem to find anything about this. I'm guessing that spyder uses a separate install of python and that installing modules via the terminal installs them only to the system version of python. How do I add them to spyder?

Specs:

Mac OSX 10.7.4

Spyder 2.1.9

Daniel
  • 2,849
  • 4
  • 24
  • 33

12 Answers12

96

Ok, no one has answered this yet but I managed to figure it out and get it working after also posting on the spyder discussion boards. For any libraries that you want to add that aren't included in the default search path of spyder, you need to go into Tools and add a path to each library via the PYTHONPATH manager. You'll then need to update the module names list from the same menu and restart spyder before the changes take effect.

Daniel
  • 2,849
  • 4
  • 24
  • 33
  • 10
    Thanks - worked for me. But also, you don't need to restart Spyder, just close the interpreter, then right-click and "open interpreter" to restart the python interpreter. The new path is used. – Sanjay Manohar Jan 27 '13 at 08:47
  • 1
    Once you implement this solution it appears that anything calling the ibpy EReader file may fail on line 29 with `SyntaxError: cannot assign to None`. If your getting this remove or comment out `None`. [Reference](https://code.google.com/p/ibpy/issues/detail?id=16) – zelusp Jun 25 '15 at 15:32
  • Under menu Spyder/PYTONPATH Manager – Ozgur Ozturk Jun 16 '16 at 18:20
  • After adding paths via spyder's pythonpath manager, restarting anaconda was necessary in my case. – Quetzalcoatl Aug 27 '17 at 20:22
  • This did not work for me on Windows. I had to manually add the package folder to the `PATH` variable and not `PYTHONPATH` variable – Courvoisier Oct 17 '17 at 13:02
  • Wow. That is extremely inconvenient... Thanks! – Jim O. Sep 08 '18 at 13:57
60

Using ! on the IPython console within spyder allows you to use pip. So, in the example, you could do:

[1] !pip install pymorph

Note, this is also available (though perhaps unreliably) on the Python console for Spyder versions before ~2.3.3. Thanks to @CarlosCordoba for this clarification.

atomh33ls
  • 23,172
  • 18
  • 91
  • 149
19
  1. Find the location of a module in Terminal:

    $ python # open python
    
    import pygame # import a module 
    
    pygame # get the location
    
  2. Copy-paste the module folder to the 'Spyder.app/Contents/Resources/lib/python2.7'

  3. Relaunch Spyder.app

j0k
  • 21,914
  • 28
  • 75
  • 84
user1219669
  • 191
  • 1
  • 4
12

If you are using Spyder in the Anaconda package...

In the IPython Console, use

!conda install packageName

This works locally too.

!conda install /path/to/package.tar

Note: the ! is required when using IPython console from within Spyder.

IceXist
  • 121
  • 2
  • 4
  • You can also run up the Anaconda Prompt and run the `pip install ` command there, after it finishes installing you can reload the kernel from spyder Consoles -> Restart kernel – zaf187 Jun 24 '19 at 15:58
8

just use '!' before the pip command in spyder terminal and it will be fine

Eg:

!pip install imutils
Joish
  • 1,104
  • 14
  • 20
3

You can add the Standard Installation location to the PYTHONPATH manager. This way you don't need to add a specific path for each module. Only to update module names.

On Unix this location is usually:

/usr/local/lib/pythonX.Y/site-packages

On Windows:

C:\PythonXY\Lib\site-packages

See the modules install Documentation.

João Almeida
  • 3,078
  • 2
  • 16
  • 33
3

I faced the same problem when trying to add the seaborn module in Spyder. I installed seaborn into my anaconda directory in ubuntu 14.04. The seaborn module would load if I added the entire anaconda/lib/python2.7/site-packages/ directory which contained the 'seaborn' and seaborn-0.5.1-py2.7.egg-info folders. The problem was this anaconda site-packages folder also contained many other modules which Spyder did not like.

My solution: I created a new directory in my personal Home folder that I named 'spyderlibs' where I placed seaborn and seaborn-0.5.1-py2.7.egg-info folders. Adding my new spyderlib directory in Spyder's PYTHONPATH manager worked!

Guy
  • 160
  • 8
  • 1
    I'm sorry for commenting on an old answer, but I was having this exact problem today with Spyder and seaborn, and there wasn't a single answer on any website which helped except yours. So, thank you. – la femme cosmique Jul 20 '16 at 10:38
  • As an addendum to my previous comment (I hope you see this!) how did you get around the issue where the spyder interpreter now takes seaborn's __init__.py as being the __init__.py of whatever I'm trying to use? In this case it broke mpfit and a few other things. Did you ever find another solution? – la femme cosmique Jul 20 '16 at 14:46
  • I'm glad it worked... to some degree. I don't recall running across init.py issues, so I'm not sure what to recommend. I recently installed a new OS and freshly installed python 2.7, anaconda and spyder. Now seaborn miraculously works from a fresh conda command line install - no longer a need for the workaround I presented above. – Guy Jul 21 '16 at 13:06
0

One can also follow the below steps : Spyder -> Tools -> Open Command Prompt -> write the command "pip install html5lib"

Waqas Bukhary
  • 4,339
  • 4
  • 38
  • 52
gauravm999
  • 11
  • 1
0

This is assuming a Conda Environment. At a high level, what worked for me was simply configuring my Conda path in Spyder. Here is how I did it:

First, determine the path your env exists at

  1. Create your environment

  2. In the Anaconda navigator, click to "environments" and then hit the play button on the environment you want to open.

  3. Click "Open with Python," you should get an interactive Python shell

  4. Type "import numpy" (choose any package)

  5. Type "numpy" and take a look at the path that looks like this:

C:\\Users\My Name\\.conda\\envs\\pytorch-three\\lib\\site-packages\\numpy\\__init__.py

The important part is the path all the way down to site-packages

For Spyder to be able to read your packages, do the following within Spyder.

  1. Open Spyder from anywhere

  2. Click "tools" and "preferences"

  3. In your Python Interpreter click "Use the following Python interpreter"

  4. From the path above, navigate to your environment and select the Python executable. For me it was here: C:\\Users\My Name\\.conda\\envs\\pytorch-three\\python.exe

  5. Finally, add the C:\\Users\\My Name\\.conda\\envs\\pytorch-three\\libs\\site-libs folder to the path (which will exist in your environment). This is easily done through the little Python icon with the tooltip of "add to path"

I personally didn't need to restart my IDE, but you may need to.

Austin Peña
  • 98
  • 1
  • 8
0

This worked for my purpose done within the Spyder Console

conda install -c anaconda pyserial

this format generally works however pymorph returned thus:

conda install -c anaconda pymorph Collecting package metadata (current_repodata.json): ...working... done Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): ...working... done Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.

Note: you may need to restart the kernel to use updated packages.

PackagesNotFoundError: The following packages are not available from current channels:

  • pymorph

Current channels:

To search for alternate channels that may provide the conda package you're looking for, navigate to

https://anaconda.org

and use the search bar at the top of the page.

Loonhaunt
  • 21
  • 5
0

You can run:

pip install pymorph

But you need to run that command in the anaconda terminal of your environment. For example:

enter image description here

Ignacio Alorre
  • 6,038
  • 6
  • 50
  • 76
0

Try This One ..

  1. Get Your Python Version from CMD enter image description here

  2. Make Sure You are using Same version in Spyder kernel enter image description here

if the version are different make it identical from "Tools>Preferences>Python interpreter> "manage the python path where your libraries are installed.

DaveL17
  • 1,018
  • 2
  • 18
  • 23