skip to Main Content

I’m experiencing an issue with installing and using the ‘sympy’ module in my Python environment. I created the environment using MiniConda and I’m using VS Code as my code editor. I have already tried installing ‘sympy’ using both Conda and pip commands, and they both indicate successful installation. However, when I try to import ‘sympy’ in my Python code, I get a 'ModuleNotFoundError: No module named 'sympy'' error.

I have double-checked that the module is indeed installed in my environment (everywhere sympy is listed as installed), but it seems that VS Code is unable to recognize it. I have restarted my computer and reactivated my environment, but the issue persists.

Could someone please help me troubleshoot this problem? Any suggestions or insights would be greatly appreciated!

import math

from sympy import*

x= Symbol('x')
l = Symbol('l')
t = Symbol('t')
e = Symbol('e')

This is the code I tried to make working.

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import math
----> 3 from sympy import*
      5 x= Symbol('x')
      6 l = Symbol('l')

ModuleNotFoundError: No module named 'sympy'

And this is the Error in detail.

Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks so much for all of your suggestions.

    I ended up updating Python to 3.11 in both Anaconda and VS Code and created a new environment using Python 3.11. Now it works just fine.


  2. I believe I have discovered the root of the problem!

    When you run code in the editor, VSCode simulates a terminal. It types enters into that terminal a line that looks something like this:

    python -u "/path/to/test.py"

    Now that python may be version 3, 2, 3.8, 3.10, etc. what you need to do is make sure that the version of python you installed scipy with matches up with the version of python your program is being run in. You can do that by doing the following:

    1. Open the VSCode terminal and type this in:

    python -m pip install scipy

    1. It will output some stuff while downloading scipy. Here’s what it shows for me:

    Collecting scipy
    Downloading scipy-1.10.1-cp38-cp38-macosx_10_9_x86_64.whl (35.0 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 35.0/35.0 MB 8.1 MB/s eta 0:00:00
    Requirement already satisfied: numpy<1.27.0,>=1.19.5 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from scipy) (1.24.3)
    Installing collected packages: scipy
    Successfully installed scipy-1.10.1

    1. As you can see on that 4th line of output, it tells me the python version, 3.8. So this means that whenever VSCode runs python, it is running python 3.8. We need to make sure that we are installing scipy with and, in fact, running our code in python 3.8.

    2. Press CTRL+Shift+P or CMD+Shift+P depending on your OS, to open the command palette. Search for the ‘python interpreter’ option. Change your python interpreter to the version mentioned when installing scipy with pip.

    That should do it!
    Remember to always install using python -m pip install

    import scipy
    
    print(scipy)
    

    Output:

    <module 'scipy' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/__init__.py'>
    
    # Success!
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search