Execute python script with file path as argument - python

I'd like to run python script under windows from command line
this is something like:
script_name.py C:\file\path
but it says SyntaxError: invalid syntax
and highlight 'C' symbol
this script is in C:\Python27\ folder
the code of script downloaded from here https://github.com/faucamp/bootstrap_namespace_prefixer

You need to type that into the Windows command prompt, not the Python shell.
And you shouldn't put your own code into the Python27 folder.

you can change directory from command prompt to C:\Python27\ then run python C:\Python27\folder\scriptname.py (path to script)
OR
Set environment variable then change directory to C:\Python27\folder. run python scriptname.py

Related

Calling a python file of virtual environment from applescript

I have got an applescript and a python script.
The applescript should be the frame of the python script and should execute the python script.
The python script uses a python version and packages, that are saved in a virtual environment.
How can I make the applescript run the python script inside of the virtual environment, so that all the packages and python versions of this environment are used?
My way to do this without applescript was to type source virtualenvironment/bin/activate and after that python /Users/abc/script.py into the terminal.
Using the applescript command
do shell script "source virtualenvironment/bin/activate"
do shell script "python /Users/abc/script.py"
does not work for me. Thanks in advance for your help !
You don't have to activate a virtualenv; that mostly just sets your PATH environment variable so that when your shell looks up what executable to use for the python command, it finds virtualenvironment/bin/python before any other python executables. Just use the expanded, full path, so /virtualenvironment/bin/python instead of python:
do shell script "v/irtualenvironment/bin/python /Users/abc/script.py"
You can also make /Users/abc/script.py executable by making the first line a shebang pointing to your virtualenv Python executable:
#!/virtualenvironment/bin/python
and setting the executable flag on the file (chmod +x script.py, from a terminal).

I am able to run python on my command prompt of windows but unable to run it in terminal of atom editor

When I tried to use python command in my terminal of atom it shows such error.
And when same thing I tried through command promt it works. plzz help me out.
Is this your Python directory? i.e. is there a file called 'python.exe' in there?
As far as I know, atom should inherit the PATH variables from the command prompt (I may be mistaken). If you restart atom, does this persist?
And also - when you tested it with command prompt, was it pointing to the same directory? Essentially, it's not opening because it needs to either
a) see a Python executable in the current directory, or
b) know where to find the Python executable (PATH variable)
Adding python to PATH:
https://superuser.com/questions/143119/how-do-i-add-python-to-the-windows-path

How do I run the python files from command shell/command prompt?

Really frustrated with this as one thing works at one time. Sometimes import filename.py works. But in the tutorials all I see is python filename.py. But when I try to give that, I am facing an error like invalid syntax.
I have edited all the environment variables and I have C:\Python27 folder in the same location. To be able to run the files using python filename.py what are the conditions that must be met? What should be the current working directory? Should the .py files be there in the same working directory?
import name is a python keyword for use within a python script and/or the python interactive interpreter (repl).
python filename.py is a shell command for use at a command prompt or within a shell script to run the python interpreter on a given python script file.
The working directory does not matter other than for whether the file listed in python filename.py can be found.
So for python filename.py to work you must be in the same directory as filename.py but you could just as readily use python c:\users\user\some\other\path\filename.py in which case the current directory isn't used to find the file.
If you get python syntax errors from attempting to run python on a python file that's a python error in the code and you will need to look at the python file to see what the error is.
Just to be clear, typing python filename.py only works from the Terminal (i.e. cmd.exe, Windows PowerShell, the "Terminal" application on a Linux kernel, etc.), not from the Python interpreter (i.e. python.exe), and only works if you have used the cd command to change into the directory in which the file is saved (so that the terminal knows where to look for filename.py). import filename can be used from the Python interpreter, but is not the ideal method as it creates a compiled version of filename.py and can only be used once (you would have to restart the interpreter to do so again). I'm not sure whether this works in the official Python distribution available from the Python website, but at least in the Anaconda distribution, you can run a file from the Python interpreter using runfile("C:/Users/CurrentUser/Subfolder/filename.py").

No command running on Python 2.7.6

I recently installed Python 2.7.6 for 64 bit Windows7. I am trying to run few commands like cd, pwd etc and it is not running either on Windows Command Prompt, or Python Commnad Line. and i am getting the follwoing error:
>>cd Desktop
File "<stdin>", line 1
cd Desktop
^
I am also not able to execute any .py codes
(I cannot post image as i dont have enough reputations)
The Python interpreter doesn't accept shell/batch commands, but only valid python code. What you are trying to do in your example would translate like this in python:
import os
os.chdir("Desktop")
What you did is start the Python interpreter in interactive mode (or maybe did you start IDLE). To run a python script saved in a file from the command line, try
C:\Python33\python.exe your_script.py
(change the path to python.exe according to your installation). If you installed Python fully, you should be able to launch scripts by double clicking on .py files.
If you use IDLE, see the official documentation. You can open a script from the menu and run it.
(Note that pwd is not a valid command for the Windows shell either (use cd without argument instead.)

Cannot get python on mac osx to reference my scripts folder

Am pretty new to python although have programmed a lot before. I'm using mac osx snow leopard and python 2.6.1.
I've followed this post about setting your PYTHONPATH to a custom scripts directory but can't get python to recognise it.
How can I run my python script from the terminal in Mac OS X without having to type the full path?
So i've got a simple helloworld script in /Users/richn/Documents/scripts/ called hello.py
Inside is this
#!/usr/bin/env python
print "Hello World!"
I've created a .profile file in my home directory with this in it
export PYTHONPATH=/Users/richn/Documents/scripts
I've also changed permissions of the file to make it executable with chmod a+x hello.py
Running ./hello.py in the terminal from that scripts folder works fine however whenever i run it outside of that folder i get this error
-bash: ./hello.py: No such file or directory
How can i get my scripts to run outside of that folder? Anyone got any ideas?
Thanks very much
What you'll want to do is to edit your PATH variable which is a list of directories your command shell checks when you run a command that does not begin with / or ./ rather than PYTHONPATH:
export PATH="$PATH:/Users/richn/Documents/scripts"
After you have exported your PATH variable you should be able to confirm that it has exported correctly:
echo $PATH
and afterwards you should be able to run "hello.py" successfully.

Categories