Python 2.7 installation from Python script - python

I have two questions:
There is an automated python script. Initially it should install Python2.7 if already installed version is lower than 2.7. (already implemented)
How can i successfully and cleanly change the python interpreter without disturbing python dependencies on the system. So that the next time any python script runs, it runs with python2.7
Linking python to python2.7 worked but it disturbs system dependencies and i cannot run modules e.g yum
Is there a way to continue with the remaining python script (after python2.7 installation and using python2.7 interpreter for rest of the code) without breaking the sequence or exiting the code?
Looking forwards to your responses.

Your best bet is to use a virtual environment. Short of that, you could try adding a "shebang" to the top of the Python script. The recommended shebang for specifying the "default" Python 2 interpreter is #!/usr/bin/env python2. Shebangs essentially allow the author of the script to specify which installation of the Python interpreter should be used when invoking this script directly (so if your script is located in script main.py, you would invoke it with ./main.py as opposed to python main.py in order for the shebang to take effect).

Related

Use Python software without Python interpreter installed on my system

I'm new to Python and I'm trying to understand the basics things.
Before to install the python interpreter on my computer I downloaded Deluge (A torrent client written in Python) and it works without any issue, then I thought I must had Python running but if I run the command:
python --version
I get the error saying that python is not installed on my computer. How is it possible ? How is the code from Deluge executed ?
The software you've downloaded probably has python embedded in its distribution, hence it doesn't require you to install it separately.
Also, running python --version just tells you that python executable isn't in any directories on your PATH variable. It doesn't say it's not installed.
If I look at MacOS Deluge distribution, it has python bundled with it.
Generally, you need python interpreter to run any python programs.
That's probably because what you've download is an executable and does not require Python to be executed.

Python 3 installation on windows running from command line

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.
EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
To run Python 2 executable:
C:\> py -2
To run Python 3 executable:
C:\> py -3
where py is a Python launcher that is bundled with your Python 3 installation.
py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:
C:\> py your_script.py
and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.
is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?
OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.
Is there some kind of way to configure Python so that it runs like this?
If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:
"C:\path to\Python 3.X\python.exe" %*
You likely missed the checkbox at the bottom of the installer.
Full documentation here: https://docs.python.org/3/using/windows.html
Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.
You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.
I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:
> SelectPython C:\Path\To\Desired\Version\Of\Python
> python
or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:
> SelectPython C:\Path\To\Desired\Version\Of\Python python -c "import sys;print(sys.version)"
You may find it helpful.

When running a python script from the console what is the difference between py vs python

I have been reading about python, and I have certain doubts when using the shebang line. When I run:
py file_name.py
It executes the script using the python version that I have indicated in the shebang line, but when I execute,
python file_name.py
The last version of Python is used instead of the one that I have specified. I would like to know the difference between using python and py when running a script from the command line. My shebang line was #! python3.4
Does using either python or py have different implications or eventually both are the same?
(This answer assumes you're using Windows, but I expect it mostly applies to other OSes too, modulo some details)
"python.exe" is the actual interpreter. You have one for each version of Python on your system. Which version gets executed when you run "python" on the command line, depends on your PATH environment variable and your current working directory.
"py.exe" is the Python Launcher. You probably only have one, even if you have multiple Python installs. Mine is in C:\Windows. It looks at the script, decides what version it's using, and delegates the actual execution to the proper interpreter.
To see what version of Python are in use for each command, run these two commands:
python -c "import sys;print(sys.version)"
py -c "import sys;print(sys.version)"
If both show the same version, then on your system they are both currently the same. However, one or the other might be updated. I personally recommend to rely on neither, and to call the Python version that you want explicitly in your scripts.

How to invoke a specific Python version WITHIN a script.py -- Windows

What line of text should I place at the top of a script.py to invoke the specific version of Python that I need to use?
I have two versions of Python on Windows XP, 2.6.5 and 2.7.2. They each have their own special modules and were installed by separate applications. My scripts are placed on the desktop so that I can double-click and run them conveniently.
The problem is that all my scripts invoke 2.6.5, which is fine for the scripts that use the modules installed with 2.6.5, but my scripts intended for 2.7.2 don't run. They invoke the Python 2.6.5 without the modules I need to import.
I've tried typing various headers without and without the #! to invoke 2.7.2 when I need to, but either my syntax is wrong or it just isn't possible to specify under Windows. Could anyone tell me the precise syntax of the line I need to add to my script. The python.exe for 2.7.2 is stored under C:\OSGeo4W\bin
Thanks for letting me know what line to place at the top of a script.py to invoke the exact version of Python I need to use.
If you have installed Python 3.3 on your system it added a new launcher for Python scripts which means you can use a shebang line:
#!python2.7
print "Hello"
or
#!python3.3
print("World")
will both work and run the appropriate python, or you can specify a full path to a Python interpreter or create an ini file that defines abbreviations for specific Python interpreters.
See PEP 397 for the different options available in Windows shebang lines.
If you don't want to install Python 3.3 then you can also install the launcher separately.
Instead of putting the script itself on the desktop, place a shortcut on the desktop. The process is described in a techrepublic.com article. Specify the appropriate interpreter as the program to run, and list one of your .py files as a parameter in the same field.
There is not "shebang" notation on Windows.
You'll need to change the file association for .py files to use your 2.7.2 installation ("Open With", "Use application as default").

how to make my epd python as my default python in ubuntu?

I recently installed epd python distribution in ubuntu. This got installed in the folder /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64
Can you tell me how to make this python as my default python?
I get errors while running a test program (it seems my default python is different and it doesn't have numpy library, other libraries that come along epd python distribution.)
My test program is here: http://www.southampton.ac.uk/~fangohr/computing/download/python/tests/testall.py
The "default" python depends on how you're invoking it.
On Ubuntu, python is normally installed as /usr/bin/python (not /bin/python) -- which may be a symbolic link.
If you invoke the python command, e.g.:
$ python myscript.py
it will use whichever python executable is in a directory that appears first in your $PATH. You can modify your $PATH, either for your current shell:
export PATH="/some/dir:$PATH"
or for all future shells by updating your $HOME/.bashrc, $HOME/.bash_profile, or whatever. /usr/local/bin is one common place to put system-specific executables, or $HOME/bin for user-specific executables.
If you want to execute the script itself, you'll need a shebang as the first line of the script:
$ head -1 myscript.py
#!/usr/bin/python
$ ./myscript.py
...
You can edit the shebang to refer to whatever Python executable you want to use.
You can replace /usr/bin/python with your preferred Python executable, but that might cause unwanted side effects; existing Python scripts that assume /usr/bin/python is the default might break.
Another option is to change the shebang to:
#!/usr/bin/env python
which lets you execute the script directly while still using whichever python is first in your $PATH. This may or may not be a good idea; see my answer to this question for further discussion.
Default python is the one found in /usr/bin directory with the name python. Making a symbolic link of :
ln -s /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64 /usr/bin/python
Assuming that is the name of the python executable, not the installer. After you installed, use the path where you installed it. f.e /home/iai/myNewPythonInstallation
might do the trick.
Most likely default 2.7 python is occupying that name, so you need to remove that, or use another name like epdPython. Then running python scripts would happen with:
epdPython myscript.py

Categories