Python Forum
How do I pick the right python in Linux env?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I pick the right python in Linux env?
#1
How do I pick the right python in Linux env?
============================================

Thanks for reviewing my threads. I have a pyhton code developed in Windows Eclipse PyDev IDE. As I place this code in Linux environment, I am finding some challenges.

I added

#!/usr/bin/env python

to code in linux box xmlcode.py.

I find many python version like Python 2.3, 2.6, 3.2 etc

I made this code file to have execute privilege.

./xmlcode.py ==> throw file not found.

python xmlcode.py ==> not producing output

I find /usr/lib64/python2.6/site-packages/lxml in linux server

but import xml.etree.ElementTree as ET
from lxml import etree

not picking up in my code run

How do I pick the correct python version env for my code?

How do I bring xml.etree.ElementTree , lxml to above correct python version env?

What are the linux env (variable) set up required for my code to work?

Thanks for your guidance.
Reply
#2
If the file isn't found, are you running it from the correct directory? When you run python xmlcode.py, what exactly happens? We need to see your terminal output and the code as well. It's impossible to help you with so little information.
Reply
#3
There are many ways to do this
  • First thing: for which version of python was the code written? Python 2 and 3 are different and it is impossible to run python 3 code with python 2 and vice versa.
  • If you want to use python 3.2, you can normally launch the program with the command python3.2 xmlcode.py
  • You could as well use #!/usr/bin/python3.2 as the shebang line
  • If lxml is not installed for this version of python, you could install it with the command sudo python3.2 -m pip install lxml. You can also install it for a single user with python3.2 -m pip install --user lxml
  • Don't set defaults for environment variables such as PYTHONPATH and a few others because different versions of python would use the same variables and this is not what you want.
  • If you want more versions of python, such as python 3.8 for example, I strongly suggest using pyenv to manage these different versions, see this thread for example or online articles such as this one.
  • If you need to customize your python interpreter, for example by modifying sys.path at startup, there are built in ways to do it such as defining sitecustomize.py and usercustomize.py files. See the python documentation about this.
Finally, as ndc85430 said above, get some information from the interpreter that you are using. If there is no output, there is a way to understand why. Start the program from a terminal, does it produce an exception traceback? Investigate.
Reply
#4
Thanks for weighing in.

Here is the code

#!/usr/bin/python
def Process_XML(infile, inxpath, xpathdln):
    """
       Process XML Xpath
    """   
    import xml.etree.ElementTree as ET
    from lxml import etree

    tree = etree.parse(infile)
    root = tree.getroot()
    print(root)
    for dln in tree.xpath(xpathdln):    
        # Iterate over attributes of datafield
            print(dln.tag + ' = ' + dln.text)
            for df in tree.xpath(inxpath):    
        # Iterate over attributes of datafield
                    print(df.tag + ' = ' + df.text)
                    for attrib_name in df.attrib:
                                    print( '@' + attrib_name + '=' + df.attrib[attrib_name])

        # subfield is a child of datafield, and iterate
                                    subfields = df.getchildren()
                                    for subfield in subfields:
                                                 print (subfield.tag + ' = ' + subfield.text)
    return;                
                
infile1 = '<CountryData.xml>  # Given below
inxpath1 = '//Country'
xpathdln = '/data'
Process_XML(infile1, inxpath1, xpathdln) 
The above code is tested in windows Python 3.8.1, but Linux has Python 2

INPUT Data
==============

<?xml version="1.0" encoding="UTF-8"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2012</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>

The python code ==> xmltest.py
It is sitting in the home directory with u+x permission

If I use ./xmltest.py throw file not found

Running this in window make output, but Linux is not making output.

I ran with python -v xmltest.py.

I did not see any errors there as well.

Thanks for your guidance.
Reply
#5
As I asked, please post the output you're seeing in the terminal. We can't see your screen.
Reply
#6
Here is what I see in the linux terminal


/export/home/XXXXXX/Scripts$ls -l xmltest.py

-rwxr--r--. 1 XXXXXX dsx-users 1698 Jun 26 12:54 xmltest.py

/export/home/bn1pb/Scripts$./xmltest.py

-ksh: ./xmltest.py: not found [No such file or directory]
Thanks for your guidance.
Reply
#7
What about
$ python xmltest.py
Is there an error message?
Reply
#8
(Jun-25-2020, 01:59 AM)MDRI Wrote: The above code is tested in windows Python 3.8.1, but Linux has Python 2
Not anymore in new releases eg Ubuntu/Mint 20.. is Python 2 not included by default.
Quote:Python3 by default
In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.
Python 3 has been in Linux distors bye default for many year,but if you do ./xmltest.py(don't test only with this) or python xmltest.py it will call Python 2 in most distros.
Most use python3 xmltest.py.
Look at Linux Python 3 environment and pyenv Simple Python Version Management.

And go back and look your older Thread,there is no need to use one for loop for each tag element as you do now.
# Example all the child element nodes of the country element
parse = root.xpath('//country/*')
>>> for tag in parse:
...     if tag.text is not None:      
...         print(tag.text)
         
1
2008
141100
4
2011
59900
68
2012
13600
Reply
#9
Thanks for weighing in.

$ python xmltest.py ===> working alright.

Our linux OS has Python 2.6.6 .

I find *.py with execute permission can run as ./*.py

Is it true?

Thanks for guidance.
Reply
#10
(Jun-27-2020, 04:42 PM)MDRI Wrote: $ python xmltest.py ===> working alright.

Our linux OS has Python 2.6.6 .
You should not at all use Python 2.6,python 2 is dead all support/maintenance stopped after 2020.
If you type python3 -V and pip3 -V in Terminal what to get?
As a adivce should at least use Python 3.6 preferably python 3.7 or newer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 628 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Python syntax in Linux St0rmcr0w 2 44,665 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Login to NordVPN on Linux with python script AGreenPig 2 5,906 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  how to run linux command with multi pipes by python !! evilcode1 2 6,215 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  where to get portable Python for Linux (Fedora)? python001 5 6,152 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Python in Linux environment on RPI kendias 22 10,901 Sep-05-2020, 03:04 AM
Last Post: K_Research
  control a linux program with python Fifoux082 9 4,002 May-08-2020, 04:24 PM
Last Post: Fifoux082
  Python version on Linux whois1230 5 3,416 Apr-10-2020, 07:12 PM
Last Post: buran
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 2,982 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020