I setup Anaconda 2.0.0 (Win 64).
It has SymPy 0.7.5.
I configured Spyder (2.3.0rc that came with Anaconda) to use symbolic math:
Tools > Preferences > iPython console > Advanced Settings > Symbolic Mathematics
I create a new project and a new file:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
integrate(x, x)
print("Completed.")
When I run this (Python or iPython console) it does not print the integral -- it only prints Completed.
But what is weird is that while in the console that just did the run if I then re-type:
integrate(x, x)
It does print the integral.
So running from a file never prints any symbolic math but typing in the console manually does?
Can anyone help with this issue -- maybe it some sort of configuration?
Thank you!
Running a script is not the same as executing code in IPython. When you run the code in a cell or prompt in IPython, it captures the output of the last command and displays it to you. When you run a script, the script is just run, and the only thing that is displayed is what is printed to the screen.
I don't think there is a way to send the IPython display object (which would be needed to get pretty latex output) from a script, but I may be misunderstanding how spyder executes the code in IPython, or missing some hooks that it has. You can try
from IPython.display import display
display(integrate(x, x))
It is because integrate doesn't print automatically, it just returns the output. You will have to pass it to print function to get the output. Try using following code:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
print(integrate(x, x))
print("Completed.")
In Python console(or IPython console) returned statements are automatically printed.
Update: Use pprint for a nice formatted output.
Related
I have written codes but it's showing cannot import pygame module
Download pygame from here: http://www.pygame.org/download.shtml
If you are not sure which version of python you are running, execute the following code in your shell:
import sysconfig
version = sysconfig.get_python_version()
print(version) #Python3
print version #Python2
Usually, Python3 will also accept the print command without brackets, so using sysconfig is the safest way. Sysconfig will also tell you which minor version you are using (such as 3.4.5)
Once you have download pygame, tryimport pygame in your shell. If this does not work, type help('modules'). This will now list all your modules. If pygame is not in there, check in which folder all other modules are saved and move the pygame module to that file path.
You can also use sysconfig to display all the file paths python is using:
import sysconfig
paths = sysconfig.get_paths()
print(paths) #For Python2, see print method above
I don't quite understand what you mean with "And make games", but the pygame website has a great documentary and some example codes to help you learn progamming with the pygame module.
Hope I could help,
Narusan
i have successfully installed pyqtgraph library in python 2.7. I forked latest project from GitHub and then python setup.py install. I am now trying to show plots with it. I open a python terminal and start typing the following:-
import pyqtgraph as pg
import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
All of these commands are successfully interpreted.
But then i run the command to see the plot as:-
pg.plot(x, y, symbol='o')
It outputs:
<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>
And then a windows titled pythonw opens and says 'not responding' and hangs and i am unable to see any output. After long time window crashes and terminal says:
Kernel died, restarting
What could be the error? Should i have installed using .exe?
EDIT:
As pointed out below by titusjan, the problem is with the default Jupyter/Ipython notebook shipping with Anaconda which i have not been able to correct. There must be some installation problem. And i am working on Windows.
pyqtgraph plots functions based on PyQT GUI-based programming. So the task of displaying plots have to be treated as starting a GUI. As told above, when I feed the commands to the IPython terminal, it works out fine:
import numpy as np
import pyqtgraph as pg
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
As noted above, problem starts when i feed the line:
pg.plot(x, y, symbol='o')
To remedy this: either input these 2 lines in one go
pg.plot(x, y, symbol='o')
pg.QtGui.QApplication.exec_()
or immediately after the previous line pg.plot(x, y, symbol='o') input this line:
pg.QtGui.QApplication.exec_()
Alternatively we could use the default QT-GUI commands also. So we get correct plots even if we run this code:-
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
However, if one wants to avoid invoking QTGui methods explicitly, assuming one has saved the below code as xyz.py, one can run the code successfully displaying the graphics by writing on the command line: pythonw -i xyz.py. This ensures that python has been explicitly asked to run in interactive mode. pythonw is for running in windows.
import numpy as np
import pyqtgraph as pg
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
I am currently using Python 2.7 on Ubuntu 16.04 and would like the ability to plot figures using Matplotlib. However, calling any sort of plot command causes the entire script to hang at that line.
** Note ** Before marking this as a duplicate question, please consider that this issue may not be related to the backend that I am using as I have tried every iteration of this solution that I have found on the internet.
A simple script which illustrates my problem:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
plt.interactive('True')
if __name__ == '__main__':
print 'Hello World!'
plt.figure()
print 'Hello Again World!'
The output of the above script is: Hello World!
The script then hangs at the plt.figure() line, causes 100% cpu usage, and cannot be killed using Ctrl-C. I use "kill" to kill that process.
I never figured out what caused my problem, but my (poor) solution was to reinstall Ubuntu 16.04 and then matplotlib. It is working fine now.
Thanks everyone!
I am trying to get a prompt that will ask for my password but when I try to call getpass.getpass() it just freezes. I am running on Windows 7 64 bit using Python 2.7 on Canopy.
import sys
import getpass
p = getpass.getpass()
print p
It is correct that Python "effectively freezes because it can't receive the input from standard input", however, for windows you can prefix your command with winpty. Then password can be inputted correctly when started like:
winpty python fileToExecute.py
winpty provides a interface similar to a Unix pty-master in a way that communication is also possible from windows terminals.
Python "effectively freezes because it can't receive the input from standard input". See https://support.enthought.com/entries/22157050-Canopy-Python-prompt-QtConsole-Can-t-run-getpass-or-interactive-OS-shell-commands-or-Windows-process
The fix is to use a different interpreter. I switched to IDLE and fixed the issue.
Faced the same issue with getpass (mingw64) and found this simple solution.
os.system("stty -echo")
password = input('Enter Password:')
os.system("stty echo")
print("")
getpass() will freeze if python is unable to read properly from standard input.
This can happen on e.g. some Windows terminals, such as using git bash.
You can use the sys module to detect if this will happen, to avoid hanging:
import getpass
import sys
# ...
if not sys.stdin.isatty():
# notify user that they have a bad terminal
# perhaps if os.name == 'nt': , prompt them to use winpty?
return
else:
password = getpass.getpass()
# ...
I also had this on mac with both Jupyter Lab and Jupyter Notebook. For me the issue was caused by the variable name.
Naming the variable PG_REMOTEPASSWORD caused a hang but PG_PASSWORD & PG_ABCPass didn't. I don't know why that is an issue, there's nothing in docs about restrictions on what a variable can be called.
My setup up is Anaconda running Python 3.7.7
In pycharm RUN i had similar issue. When i opened files using Terminal "CMD" issue resolved.
I am running python code where I want to write some output to a particular folder (which different from the location where I execute the script).
Therefore I was planning to change the path of Python to this particular folder using the os module:
os.chdir("myLocation.../Folder")
However, the script still writes to the folder where I executed the script, and when I invoke the command
os.curdir
it returns ".".
I am a little bit lost here and would appreciate any hint.
os.chdir should do the correct thing. Here is some code used for testing on python REPL, assuming you have a ./test dir in working dir.
>>> import os
>>> os.chdir('test')
>>> f = open('testfile', 'w')
>>> print>>f, 'hello world'
>>> f.close()
test/testfile is now present with the right contents.