I'm using mip4py and I am able to run my code from the command prompt using mpiexec -n 4 python myfile.py. I want to use the 'run' button in Spyder to do the same, how do I go about integrating my custom run command (or mpi) with Spyder run?
(Spyder maintainer here) You can run your file with our Run button by putting the following code in a different file:
from IPython import get_ipython
ip = get_ipython()
ip.run_cell("!mpiexec -n 4 python myfile.py")
Note: It's very important that you use a different file to not cause an infinite recursion while evaluating the above code.
Related
I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html
This command sets an environment variable ("CUDA_VISIBLE_DEVICES") for a python script before running.
$ CUDA_VISIBLE_DEVICES=2,3 python my_script.py # Uses GPUs 2 and 3.
It works fine on a Linux machine but on Windows, it says that
'CUDA_VISIBLE_DEVICES' is not recognized as an internal or external command
Is it possible to do that on Windows without altering the python script?
For a one liner simply run on Windows:
set CUDA_VISIBLE_DEVICES=2,3 & python my_script.py
For windows, this worked for me too, thanks to Adonis. you have two options
on the command line you can use the following command
set TF_CUDNN_USE_AUTOTUNE=0 CUDA_VISIBLE_DEVICES=1 & python demo\singleperson.py
on the main python file, put the following lines, below import things
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="1"
then run the python file on the command line
python demo\singleperson.py
I am using dryscrape in a python script. The python script is called in a bash script, which is run by cron. For those who may not be aware, dryscrape is a headless browser (use QtWebkit in the background - so requires an xsession).
Here are the main points concerning the issue I'm having
When I run the python script from the command line, it works
When I run the bash script from the command line, it works too
I figured out that this may have something to do with different environments between my command prompt and when the cron job is running, so I modified my bash script to source my .profile as follows:
#/bin/bash
. /full/path/to/my/home/directory/.profile
python script_to_run.py
This is what my cronjob crontab entry looks like:
0,55 14-22 * * 1-5 /path/to/script.sh >> $(date "+/path/to/logs/\%Y\%m\%d.mydownload.log" )
By the way, I know that the job is being run (I can see entries in /var/log/syslog, and the script also writes to a log file - which is where I get the error message below):
In all cases, I got the following error message:
Could not connect to X server. Try calling dryscrape.start_xvfb()
before creating a session
I have installed the prerequisites, on my machine (obviously - since it runs at the command line). At the moment, I have run out of ideas.
What is causing the script to run fine at the console, and then fail when run by cron?
[[Relevant Details]]
OS: Linux 16.0.4 LTS
bash: version 4.3.46(1)
cron user: myself (i.e. same user at the command prompt)
dryscrape: version 1.0.1
The solution to this was to call the dryscrape.start_xvfb() method before starting the dryscrape session.
Cron user does not have display, so you cannot run any command which requires a display.
You need to modify the python script to do not use any type of display (check carefully, because some python commands, even though they do not open any display , they internally check for this variable).
The best way to test is to ssh into the machine without Display, and check if you can run it from there without erros.
I've just recently installed Python(x,y) 2.7.5.1 on my computer with no other Python distributions installed in parallel and I have a problem with the Spyder IDE.
Namely when I create a script even a simple:
print "Hello World"
and I try to run it via the usual Runfile/F5 I get the following error:
name 'runfile' is not defined
As the button seems to be trying to use a runfile() function to run the scripts I create in the IDE.
If I try to run the same scripts by hand in Spyder's console with execfile() it works.
My question is how do I get the standard Run button and implicitly runfile() to work or alternatively to redirect the Run button to use execfile() ?
The problem seems to go away if from the run configuration I choose the option that each script be executed in it's own new individual console.
I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.