简体   繁体   中英

Python SymPy solving linear system

I have Python 3.3.3 and SymPy 0.7.4.1, both in x64 version and installed locally on my computer. I am using PSPad as a configured editor for Python scripting.

When using imports from library sympy in a module which should solve a set of three linear equations:

from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z

def main():
    system = Matrix (((3,2,-1,1) ,(2,-2,4,-2),(-1,0.5,-1,0)))
    print(solve_linear_system(system, x, y,z))

if __name__ == "__main__":
    main()

The editor PSPad console output returns the following:

traceback (most recent call last):   File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\linear system solve SYMPY.py", line 1, in <module>
    from sympy import Matrix,solve_linear_system   File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\sympy.py", line 2, in <module>
    from sympy import var,Eq,solve ImportError: cannot import name var

Process completed, Exit Code 1.
Execution time: 00:00.134

Actually, I am wondering myself heavily about those issues:

  1. Why, when typing the same thing, without an object def main() , and entered line by line in IDLE, everything is solved correctly, as: {x: 1.00000000000000, y: -2.00000000000000, z: -2.00000000000000}

  2. Why, my PSPad file with object, having the same computation lines, doesn't work and returns errors?

In fact, I would like to use SymPy in normal python code and get computed results in a list or printed in console( .. as in IDLE). Just in order to avoid some annoying line-to-line IDLE manipulations, what should my code file look like?

The problem seems to be that you have created a file named sympy.py , which has the same name as the sympy module.

Hence, in the from sympy import ... statement, your sympy.py is acting as the sympy module.

Try renaming the file to something else, like sympy_programming_test.py and let know if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM