I have a python script which I am distributing in several nodes. I have python 2.6 installed in /usr/bin by default and have python 2.7 in my /opt directory in all the nodes. Now when I run the script from my current node I can set the path to python 2.7 interpreter from terminal but I am unable to manage it in the rest of the nodes where this script is getting distributed. I have added the shebang at the start of script like -
#!/opt/python2.7/bin/python
But its still not working. How can I change the python interpreter/python path at the beginning of the script itself.
What you explain should work but check:
that the script is executable (chmod +x my_script.py if required).
that you are calling the script directly and not using another Python interpreter (check that you execute ./my_script.py or /path/my_script.py and not python my_script.py).
To help to diagnose the problem you could add the following lines to the top of your script:
#!/opt/python2.7/bin/python
import sys
print(sys.executable)
if the output is not /opt/python2.7/bin/python you might be calling the script with another interpreter.
If for some reason you can only call scripts executed by the 2.6 version of Python remotely but you can also distribute additional files, you could try to send your main script somewhere and execute the following auxiliary script:
from subprocess import call
call("/opt/python2.7/bin/python /path/my_scipt.py", shell=True)
Related
I am attempting to make a command line python tool to locate IP addresses. I am using the pygeoip library. When I have the file as filename.py, and I run it using
python filename.py -options
it works fine. But when I rename it to just filename (no .py) and add
#!/usr/bin/python
To the top of the top of the file, the pygeoip module can no longer be found. I have chmoded the file properly.
Anyone come across this before?
The problem is that /usr/bin/python is not the same python where pygeiop is installed. The shebang
#!/usr/bin/python
tells the shell to execute the script with the program given. If you change it to
#!/usr/bin/env python
the shell will search the PATH for a python and use it. If your "real" python is first on the path, it is run. You could use virtualenv, or even just add your favorite python to the front of PATH to control what is executed.
For deployment, /usr/bin/env is superior because then you don't have to worry about where the user installed python. It could be in /usr/local/bin or who knows where, and the shell will find it.
When I try to run a python script with python3 it does not work but it works when I just use python. Why is this?
I have a simple hello.py file:
__author__ = 'A'
print("hellow")
When I use python ~/path/hello.py with geektool it works, but not with python3 ~/path/hello.py, the same works from terminal.
Also, where can I see geektool's log file?
From the comments, it looks like you have Python 3 installed at /usr/local/bin/python3. It could be that that's not part of the default PATH, but you've configured your login shell to add it to the PATH. Since your other program either executes the program directly or does it through a non-login shell, it won't read that configuration, and the PATH will remain at its default, excluding that directory. If that's the case, you might have to instead change your command to have an absolute path to Python:
/usr/local/bin/python3 /path/to/hello.py
This should work from the Terminal and any other environments.
I have a Python (.py) file and need to execute it from AutoIt. How can I do this?
Python scripts can be executed from command line. For a script called myscript.py, you can run it (assuming Python is installed) by typing:
python myscript.py
If you want to run myscript.py without having to prefix it by python, then set the path to the Python binary (e.g. C:\Python27\Python.exe) as Windows environment variable. This enables AutoIt to execute the Python script as if it were an external program. Reference: ShellExecute().
ShellExecute("myscript.py")
Point to where myscript.py resides of course. Use:
RunWait("C:\Python27\Python.exe myscript.py")
to avoid setting environment variables.
RunWait("C:\Python27\Python.exe filename.py")
this code working fine run from autoit
I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:
python script1.py
I've also tried adding this line to the beginning of the script:
#!/usr/local/bin/python
As well as this one:
#!/usr/bin/env python
Yet, I keep getting the same error message. Any help would be greatly appreciated.
Make sure you are in the right working directory after opening terminal. Type
cd /Users/billp/Documents/
(use tab to autocomplete)
then
python ./script1.py
This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.
The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.
Maybe you forgot to make the file executable? Try this at the command prompt:
$ chmod +x script1.py
I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:
#!/usr/bin/env python
#coding=utf-8
Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.
One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:
if __name__ == '__main__':
# the code you want to call
Are your python binaries here?
/Library/Frameworks/Python.framework/Versions/3.2/bin/python
It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.
So the following examples are calling the same file:
Explicit (absolute path)
python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py
Implicit (relative path)
cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py
As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.
I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.