starting a python 3.3. script at ubuntu startup - python

The standard python version of ubuntu 13.04 is python 2.7.
I know that I can call a python script of version 3.3 by calling python3.3 or python3 in terminal instead of only "python", which starts the version 2.7...
e.g. python3 myscript.py
But now I have a version 3.3. script in the system start routine and can only tell the path to the file. The system recognizes it as a python script (in the shebang with #!/usr/bin/python3)
But how to open it with the correct version? It is tried to be opened with the standard python install so it wont work nor even show up.

The shebang line #!/usr/bin/python3 should work if sh, bash, etc. is trying to launch your script.
It it is being run from another script as python myscript.py you'll have to find that script and get it to launch the script using python3 myscripy.py

Related

pythonw missing with Enthought's distribution of Python 3

I have a wxPython GUI that needs to run with Python 3 using both Enthought Python (EDM) and Anaconda Python.
The problem is, when using Anaconda Python I have to use pythonw or the script fails with this error:
This program needs access to the screen. Please run with a Framework build of python, and only when you are logged in on the main display of your Mac.
Previously, using Python 2.x, I solved this by specifying:
#/usr/bin/env pythonw
which worked for both Canopy Python 2 and Anaconda Python 2.
However, EDM Python 3 seems to lack pythonw.exe, so the I have to edit the shebang line to
#/usr/bin/env python
to get my script to run in with EDM python 3.
So, is there a way to use the pythonw executable with EDM python 3?
Sure. Just create a script called pythonw and put it in $PATH:
#!/bin/sh
exec /usr/bin/env python "$#"

Changing version of python when using nohup

I am trying to run a python program called compare.py with the linux nohup command which keeps the program running until it is done without interruption. my python program has packages which can only run on python 2.7 and when i use nohup command program is run in python 2.6. how do i change the version of python when using nohup?
Example: nohup python compare.py $
I tried doing:
alias python=python2.7
before starting program and version of python isn't switched. how do i switch the version of python to 2.7 when i run nohup?
The easiest way would be to use a shebang line to specify the interpretter. At the start of your Python file, put something like
#!/usr/bin/python2.7
# This should be a path to an interpreter that you know for sure is Python 2.7
Then, use chmod +x file.py to make the Python file itself executable, and omit the python part of your nohup command, eg. nohup ./compare.py.
I had the same issue with anaconda python. While using nohup python, it was using python 2.7 but generic python command in terminal was taking me to 3.6.
nohup ~/anaconda3/bin/python scriptname.py
Providing full path to python after nohup command will solve the issue

How do I run gitbash as a terminal with a python file

I installed both Python and GitBash. I was wondering how I could allow gitbash to become a terminal for a python file I would like to run.
Thanks.
I am using Python 3.4 btw.

Specify which version of Python runs in Automator?

In my terminal and in CodeRunner my Python is updated to 2.7.6 but when I ran a shell script in the OSX Automator I found that it is running 2.7.2
How can I update the Automator Python to 2.7.6 like the rest of my compilers ?
I couldn't specify explicitly which python for it to use.
So, I ran it in bash environment with following command:
$ your/python/path /path/to/your/python/script.py
And make sure first line of your python program contains the path to the python environment you wish to use.
Eg:
#! /usr/local/bin/python

Using "Edit with IDLE" with a Python 2.6.5 script

I have installed both Python 2.7.1 and 2.6.5 versions on Windows. I have added only the path of Python 2.6.5 in the Environment Variables.
Now, I want to run a Python script x.py using Python 2.6.5. I know how to do it using the cmd but It would be more convenient to just open it with IDLE and run inside it using the Run Module option.
This is supposedly done by right-clicking over the script, and then going to Edit with IDLE option, but this opens and runs the script using Python 2.7.1. Is there a way to open and run it with Python 2.6.5?
The standard command in the registry for Edit with IDLE is the following:
"C:\Program Files\Python33\pythonw.exe" "C:\Program Files\Python33\Lib\idlelib\idle.pyw" -e "%1"
Now, as you can see, the path is hardcoded into it, so it just cannot use a different Python interpreter like that—at least per default.
However, PEP 397 introduced a new Python launcher for Python making it possible to launch different versions of Python based on the shebang line. So a file starting with #!/usr/bin/env python2 will launch the current Python 2 interpreter, while #!/usr/bin/env python3 will use Python 3.
Using that information, you can launch IDLE for a given Python version dynamically. For example this would edit the file using the launcher’s default Python version:
C:\Windows\pyw.exe -m idlelib.idle -e "%1"
This would force the use of Python 3
C:\Windows\pyw.exe -3 -m idlelib.idle -e "%1"
And this would force the use of Python 2:
C:\Windows\pyw.exe -2 -m idlelib.idle -e "%1"
So what you would need to do is write a script that basically checks which Python version a file wants to be executed with, i.e. by parsing the shebang line manually (sadly the launcher does not give you this information without actually launching the script—yet. I might start a pull request to get such a feature into the core). Then you would run either the Python 2 or Python 3 IDLE using the command above and done.
You would just need to change the Edit with IDLE command to execute your script then and it would work.
A very simple alternative would be to just add another registry key which launches the Python 2 IDLE. So you would have Edit with IDLE and Edit with IDLE (Py2) or something.
To do that, just put the following inside a .reg file and execute it:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)\command]
#="C:\\Windows\\pyw.exe -2 -m idlelib.idle -e \"%1\""
edit: I just noticed that I wrote this mainly about Python 2 vs. Python 3; the launcher ships with Python 2.7 I think, so this will work just the same. You just need to adjust the launcher’s version specificiers to -2.6 and -2.7 or whatever you want.
You can do this with some registry hacks to make IDLE 2.6 your default (rather than 2.7), but that's not really what you want, I think, since then you'd have to reverse the process when you want to test something in 2.7. Unless someone else knows some other way to integrate different IDLE installs into the shell, here are a couple better options:
Open IDLE 2.6 first, and just use the Open File dialog from the GUI.
Use a different IDE that actually supports this functionality. Eclipse with PyDev will let you switch interpreters between runs, or save configurations with different interpreters, and so on.

Categories