Saving to particular directory from Python 2.7 - python-2.7

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.

Related

Make Python script globally executable

Let's say I've got this one-line Python module called say_hello.py.
print 'Hello World'
How can I make the script executable from any location in my terminal? That is, having Hello World printed outside the Python interpreter anywhere on my system. I'm running on OS X Mavericks.
General *nix answer
First line of script should look something like:
#!/usr/bin/python
although the exact path may be different on your system. Then, make the script executable and put it somewhere in your PATH.
Add as the first line of your script:
#!/usr/bin/env python
or, for a python3 script:
#!/usr/bin/env python3
The shell (actually the kernel) will use the first Python/Python3 interpreter found in your $PATH.

Spyder SymPy Wont Print Symbolic Math

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.

Beginner Python spam.py

I am new to python, just got the learning python book and got stuck with the spam.py in the command line. The book says to make a file named spam.py and then ask python to run this by typing
%python spam.py
I have added the python to my PATH as it was C:\Python27 so I can call Python in the Windows CMD, but this just will not run. The error I receive is
>>>python spam.py
File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
I appreciate any help that you can give.
Your problem is that you're trying to run your code from within the Python interpreter itself (the >>> prompt is the giveaway here since that's the Python prompt).
Exit from the interpreter (with CTRL-Z and ENTER for Windows) and run it from cmd.exe (the c:\> is the prompt in the example below):
c:\> python spam.py
From within the interpreter, you can also run an external file with:
execfile('spam.py')
Could you post the code from spam.py...
You seem to be trying to run the spam.py from the Python interpreter. Go to where the file is in Windows Explorer and launch it from there, using the C:\Python2.7\python.exe CLI.
By the way, since you didn't understand the syntax error warning, please see:
http://dictionary.reference.com/browse/SYNTAX
http://dictionary.reference.com/browse/SEMANTICS
You are trying to execute Python script file within the interpreter. Come out from the Python interpreter by pressing CTRL+Z and then ENTER key.
Then execute with the command :
Say, C:/> python spam.py
In order to run a python program you have to run program in Command Line not in Python Interpreter (press Windows Sign + R and type cmd.exe)
Moreover you have to remember to be exactly in the directory where your file is saved, e.g.:
If the file file is C:\Python27\spam.py you have to be in C:\Python27.
To change the directory:
use dir to display the folders and files in current place
use cd to change your directory (e.g. C:\Python27\>cd Spam moves you to C:\Python27\Spam
use Tab key to autocomplete names of the commands, folders and files
As you said you have added Python to PATH and followed my instructions, the statement below should work perfectly
python spam.py
Hope I could help.
If for example, your file is in G: drive, type this in cmd:
python G:/myfile.py
Basically, type in the path. Just doing "cd" won't work in Python

How do I run my Python program that I wrote?

I am trying to run my celcius.py file but I don't know what to type in the command line...
When I type Python celcius.py it does not work:
I get the following message:
>>> python celcius.py
File "<stdin>", line 1
python celcius.py
^
SyntaxError: invalid syntax
>>>
When I open it the program with the Python IDLE GUI it runs the program and closes the program before I can see it. I read somewhere that it won't close if you run it from the command line.
You need to type "python celsius.py" in the windows command prompt, rather than the Python command prompt; this only works straight away if the Python directory is in your PATH. It probably isn't, so you need to use this instead: C:\Python27\python.exe celsius.py.
Because celsius for you is in the same folder as python, you can do this as well:
CD C:\Python27
python.exe celsius.py
In general, change the directory (CD) to where your file is stored, then run C:\Python27\python.exe <filename>
Double-clicking is generally easier in Windows, though, assuming it is set up to do that (it normally is unless you change it back)
There are a few ways of doing this.
WAY 1
step one: Open command prompt (go to start and type in "cmd") *NOTE: do NOT open python. if you see ">>>" in your command prompt window you opened python. This is bad.
step two: type in this: python "path\to\file\celcius.py" replacing path\to\file with the path (starting with the name of the folder that you're computer user is named. So for my computer that would be ben. So let's say my celcius.py is located on the desktop I would run python "Desktop\celcius.py" and hit <.enter.>. It looks like you have celcius.py in your python folder. Don't put it there. that's the system files for python itself. move it to your desktop or your documents folder. By the way, if you have truble using this, make sure you're system is set up to execute the python command. Go here for reference.
WAY 2
assuming you have python associated with the .py file in explorer, you can find the file and double-click on it.
WAY 3
open IDLE (you can usually find it in your list of programs in start). Open the python file through IDLE. This will show you the source code for your program. hit <.F5.>. F5 will execute the program from within the the IDLE shell.
You don't need to do this, just open the command prompt and type:
python path/to/your/file.py
Just make sure that python command is set in your environment PATH, check this

How to execute python script from a module I have made

I have written a python27 module and installed it using python setup.py install.
Part of that module has a script which I put in my bin folder within the module before I installed it. I think the module has installed properly and works (has been added to site-packages and scripts). I have built a simple script "test.py" that just runs functions and the script from the module. The functions work fine (the expected output prints to the console) but the script does not.
I tried from [module_name] import [script_name] in test.py which did not work.
How do I run a script within the bin of a module from the command line?
Are you using distutils or setuptools?
I tested right now, and if it's distutils, it's enough to have
scripts=['bin/script_name']
in your setup() call
If instead you're using setuptools you can avoid to have a script inside bin/ altogether and define your entry point by adding
entry_points={'console_scripts': ['script_name = module_name:main']}
inside your setup() call (assuming you have a main function inside module_name)
are you sure that the bin/script_name is marked as executable?
what is the exact error you get when trying to run the script? what are the contents of your setup.py?
Please check your installed module for using condition to checking state of global variable __name__.
I mean:
if __name__ == "__main__":
Global variable __name__ changing to "__main__" string in case, then you starting script manually from command line (e.g. python sample.py).
If you using this condition, and put all your code under this, it will be be work when you will try to import your installed module from another script.
For example (code from module will not run, when you will import it):
testImport.py:
import sample
...another code here...
sample.py:
if __name__ == "__main__":
print "You will never see this message, except of case, when you start this module manually from command line"