bat script to compile py2exe - python-2.7

hello i know this is kind of a dumb question but i need help writing a bat script to make a python file run it then delete it all of this is simple but i cant seem to get it to work my code:
#echo off
set /p %file% = type the file you want to make to exe:
echo from distutils.core import setup >> setup.py
echo import py2exe>> setup.py
echo setup(console=['%file%'])>> setup.py
Python-Portable.exe setup.py py2exe
pause
del "setup.py"
thank you!

set /p "file=type the file you want to make to exe: "
Resources (required reading):
SET command
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax

Related

Calling Python package from command line / PyCharm

I am creating a Python package, I anticipate that it will be called both by command line and from other scripts. Here is a simplified version of my file structure:
GREProject/
__init__.py
__main__.py
Parsing.py
Parseing.py contains a method, parse(), it takes two arguments, an input file and an output file. I am trying to figure out the proper code for "__main__.py" so that when the following is called from the command line or terminal the arguments will be passed to "parse()":
Python GREProject -i input.file -o output.file
I have tried this numerous ways but they have all met with failure, I do believe I need the "-m" flag for the interpreter but more than that I don't know. Example with the flag:
Python -m GREProject -i input.file -o output.file
When running the later command I receive the following error:
Import by filename is not supported.
Presumably from this line:
from . import Parsing
Ok, turns out this was a problem with my IDE, PyCharm. No idea why I recieved this error but I have setting that fixed it:
Import by filename is not supported.
For the record here are the options I set in my Pycharm project
Script:
GREProject
Script parameters:
-i .\GREProject\pr2.nyc1 -o .\GREProject\Test.pkl
Enviroment variables:
PYTHONUNBUFFERED=1
Python interpreter:
Python 2.7.11 (c:\Python27\python.exe)
Interpreter options:
-m
Working directory:
C:\Users\probert.dan\PycharmProjects
Here is an explanation of the options:
Script: This is the script to run, by default PyCharm will only insert absolute references to .py files, nothing prevents you from manually typing in a relative reference, in this case it is the GREProjects folder.
Script Parameters: These are passed onto the script itself, in this case I am telling my script that the input file is ".\GREProject\pr2.nyc1" which means, look the file "pr2.nyc1" in the "GREProject" directory below the current working directory.
Environment variables: This was set by PyCharm and left unchanged.
Python interpreter: My active interpreter.
Interpreter options: The option here tells python that we are calling a module, python then knows to access the "__main__.py" file.
Working directory: The directory the script is run from, I chose the directory above "GREProject"
For reference here is the contents of my "__main__.py file":
from . import Parsing
import argparse
parser = argparse.ArgumentParser(description='Parse flags.')
parser.add_argument('-i', help='Import file.')
parser.add_argument('-o', help='(Optional) Output file.')
arguments = parser.parse_args()
Parsing.parse(arguments.i, arguments.o)
It is also important to note that debugging in PyCharm is not possible like this. Here is the solution to debugging: Intellij/Pycharm can't debug Python modules

How to run executable with input file using Python?

I am running an executable from cmd:
*.exe input.inp
I want to run it using python and tried following:
os.system('"*.exe"')
But don't know how to specify input file as well. Any suggestions?
import os
from subprocess import Popen, PIPE
p = Popen('fortranExecutable', stdin=PIPE) #NOTE: no shell=True here
p.communicate(os.linesep.join(["input 1", "input 2"]))
For more please refer to:
Using Python to run executable and fill in user input
I had to launch cmd window and specify input file location from within Python script. This page was really helpful in getting it done.
I used Popen(['cmd', '/K', 'command']) from above page and replaced '/K' with '/C' in it to run and close the cmd window.
import os
os.system(r'pathToExe.exe inputFileOrWhateverOtherCommand')

learning python hello world script not executing

I am new to Python and learning to program in it.
I wrote a basic script
my.py
#! /bin/python
print "hello world!"
but this does not execute.
I did
chmod +x my.py
and
./my.py
I have tried removing the blank space in my program
#!/bin/python
but even this gives me error
bash: ./my.py: /bin/python: bad interpreter: No such file or directory
I am using Ubuntu 12.04 and Python is present on system.
I have checked the program by typing on terminal
So what could be the problem here?
The error means there is no file at the path /bin/python
Try which python to see the location of the interpreter
But why not python my.py?

how to use shedskin to convert python code to c++ code?

I read I document provided but can't understand how to use shedskin, I'm not experienced at python.
I have python 2.7, I test it seems it works in cmd, or if I just double click on .py file it produce .pyc file.
I run tests in shedskin\shedskin-0.9.1\shedskin\tests by clicking run.py it produce some .cpp and makefile, but I can't understand how to run it on my .py files?
mkdir helloworld
cd helloworld
echo -e "import sys\nprint 'hello '+sys.argv[1]" > helloworld.py
python helloworld.py this_is_python
shedskin helloworld
make
./helloworld this_is_c_plus_plus

Creating executable from python source file

I am following this guide for python 2.7 :
http://docs.python.org/tutorial/interpreter.html
I do all it says: I have a python file:
#! /usr/bin/env python
print "hello world !\n"
And from terminal, in the directory where is the file I type:
chmod +x hello_world.py
The file is name hello_world.py; But nothing happens, it doesn't print "hello world\n".
sorry if this is insultingly obvious, but
> chmod +x hello_world.py
only changes the file so that you can run it. next you need to actually run the file by typing:
> ./hello_world.py
hello world !
To give a bit more description: the chmod command changes the permissions of a file on a Unix-style system. The +x in the command:
chmod +x hello_world.py
Sets the "Executable" bit for the hello_world.py file, thereby making it a script which can be executed. Thus to run the script:
./hello_world.py
The ./ in front indicates that the file is in the current directory. Alternatively, you can always run a script by invoking the python interpreter directly (regardless of permissions) like so:
python hello_world.py