Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

[OT++] Seeking Win32 Python Wisdom

by syphilis (Chancellor)
on Mar 16, 2017 at 11:33 UTC ( #1184865=perlquestion: print w/replies, xml ) Need Help??
syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
For this simple 2 line python script:
import sysconfig print(sysconfig._getuserbase())
my python-2.7 outputs C:\Users\sisyphus\AppData\Roaming\Python
I am seeking a win32 python-2.7 that, for the same script, would output forward slashes in place of those backslashes.
Anyone know where I might obtain such a beast ?

Alternatively, is there a file somewhere inside my existing python-2.7 distro that I could hack such that those backslashes are replaced by forward slashes ?

Cheers,
Rob

Just to explain a little:

My current building (in the msys2 shell) of gobject-introspection (needed for the perl module Glib-Object-Introspection) fails to run to completion. It dies with:
./libtool: line 5999: cd: C:_32Python27/lib: No such file or directory
Clearly, a path of C:\_32\Python27/lib has been delivered to the build process - and I'm fairly sure that those backslashes come straight from python itself. Certainly, I haven't specified any paths that contain a backslash.
Now, the msys2 shell will understand /c/_32/Python27 and C:/_32/Python27 but it regards the backslashes in C:\_32\Python27 as escapes.

Here's a quick demo of this:
sisyphus@Owner-PC311012 MINGW64 ~ $ ls /c/_32/Python27 DLLs include libs NEWS.txt pythonw.exe tcl w9xpopen.exe Doc Lib LICENSE.txt python.exe README.txt Tools sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:/_32/Python27 DLLs include libs NEWS.txt pythonw.exe tcl w9xpopen.exe Doc Lib LICENSE.txt python.exe README.txt Tools sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:\_32\Python27 ls: cannot access C:_32Python27: No such file or directory
So ... I'm thinking that if I can get python to deliver paths that contain only forward slashes then this problem will be solved.

Until then, all is *not* necessarily lost. The build does proceed long enough to create the girepository library - which might just turn out to be all that Glib-Object-Introspection needs.
In any case, it would be nice to get the damn process to work as it should (even if only to enable installation by running make install, instead of the manual copy'n'pasting that's currently needed).

Replies are listed 'Best First'.
Re: [OT++] Seeking Win32 Python Wisdom
by thanos1983 (Priest) on Mar 16, 2017 at 12:35 UTC

    Hello syphilis,

    Why don't you try to get the path and then convert it?

    Sample of code:

    #!/usr/bin/python import sysconfig sysconfig_output_before = sysconfig._getuserbase() sysconfig_output_after = '\\'.join(sysconfig_output_before.split('/')) print sysconfig_output_before print sysconfig_output_after

    Sample of output:

    /home/user/PythonVirtualEnvironment/bin/python /home/user/Downloads/Py +thonProject/test.py /home/user/.local \home\user\.local

    Update: Maybe also this could help (Windows shell string operations (changing backslash to slash)). I do not have WindowsOS so I can not really test it (sorry).

    Update2: I remember many many years ago when I was running WindowsOS I used to use (Cygwin). If I remember correctly it uses forward slashes, again (sorry) I do not have WindowsOS so I can not really test it.

    Update3: In your case you have to reverse the parameters of course. I did not mention that before I did not thought that it is necessary.

    Update4: I also found (Kernel::System::SysConfig - to manage sys config settings). I do not know if it helps but from the documentation is more or less what sysconfig ó Provide access to Pythonís configuration information. Just an idea. UnixOS

    Update5: I also found (Win32::kernel32). I do not know if it helps but from the documentation: SearchPath can provide you:

    1. the directory from which the application loaded. 2. the current directory 3. the Windows system directory 4. the Windows directory 5. the directories in the PATH environment variable

    Just an idea and this is for. WindowsOS

    Update6: Most likely you have thought about it already but just came into my mind. Why you do not execute your Python script and capture the output and convert it with Perl?

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; my $path_before = qx(/home/user/Downloads/PythonProject/test.py 2>&1); print "Befofe: " . $path_before; my $path_new = $path_before; $path_new =~ s{/}{\\}g; # Use s/\\/\//g; print "After: " . $path_new; __END__ perl test_2.pl Befofe: /home/user/.local After: \home\user\.local

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: [OT++] Seeking Win32 Python Wisdom
by LanX (Bishop) on Mar 16, 2017 at 12:10 UTC
    can't offer help, but this

    > needed for the perl module Glib-Object-Introspection

    makes it on topic here. :)

    (just to prevent useless bashing)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      makes it on topic here. :)

      I don't think so; but it at least makes it Off Topic to an acceptable degree.

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
Re: [OT++] Seeking Win32 Python Wisdom
by stevieb (Abbot) on Mar 16, 2017 at 14:11 UTC
    "Alternatively, is there a file somewhere inside my existing python-2.7 distro that I could hack such that those backslashes are replaced by forward slashes ?"
    >>> import site >>> site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-pa +ckages']
    locate sysconfig.py /usr/lib/python2.7/sysconfig.py /usr/lib/python2.7/sysconfig.pyc /usr/lib/python2.7/dist-packages/distlib/_backport/sysconfig.py /usr/lib/python2.7/dist-packages/distlib/_backport/sysconfig.pyc /usr/lib/python2.7/distutils/sysconfig.py /usr/lib/python2.7/distutils/sysconfig.pyc /usr/lib/python3.4/sysconfig.py /usr/lib/python3.4/distutils/sysconfig.py

    That's on Linux, but the premise is the same for Windows. Briefly looking at the _getuserbase() code within the /usr/lib/python2.7/sysconfig.py file, the result may be coming from somewhere in the os.path method (os.py file), and I suspect that hacking it to do what you want will take quite some effort to make it consistent, while not breaking other stuff.

    As mentioned already by thanos1983, modifying the result as it is returned to you, or overriding the method may be your best and/or safest bet.

      As mentioned already by thanos1983, modifying the result as it is returned to you, or overriding the method may be your best and/or safest bet

      I'm dealing with an autotools (./configure) build, and the libtool script that's reporting the error is itself autogenerated during the build.

      I suppose it might be possible to make the correction in that script and then reset the script's timestamp to it's original setting.
      Line 5999 of the libtool script is:
      absdir=`cd "$dir" && pwd`
      I could try preceding that line of code with the shell scripting of $dir =~ s/\\/\//g
      What would that shell scripting equivalent of that perl code look like ? (I know as much about sh as I do about python ;-)

      It would be much better if Python just delivered a forward-slashed path.
      I'm guessing that there's a python command that's handing over python's equivalent of something like $Config{prefix}.
      C:\>perl -V:prefix prefix='c:\MinGW\perl524_64int';
      In perl I could easily hack a solution such that perl -V:prefix returned a forward-slashed rendition of that path.
      I was hoping that python might also lend itself to such hacking.

      looking at the _getuserbase() code within the /usr/lib/python2.7/sysconfig.py file, the result may be coming from somewhere in the os.path method (os.py file),

      Hmmm ... altering os.sep might (or might not) be all that's needed.
      How would I change that setting ?
      Is there a simple python command (or script) that will tell me the current setting of os.sep ?

      Cheers,
      Rob

        I've got a lot of Python dev systems at work, so I will have a closer look throughout the course of today :)

      As mentioned already by thanos1983, modifying the result as it is returned to you, or overriding the method may be your best and/or safest bet

      I'm dealing with an autotools (./configure) build, and the libtool script that's reporting the error is itself autogenerated during the build.

      I suppose it might be possible to make the correction in that script and then reset the script's timestamp to it's original setting.
      Line 5999 of the libtool script is:
      absdir=`cd "$dir" && pwd`
      I could try preceding that line of code with the shell scripting of $dir =~ s/\\/\//g
      What would that shell scripting equivalent of that perl code look like ? (I know as much about sh as I do about python ;-)

      It would be much better if Python just handed a forward-slashed path.
      I'm guessing that there's a python command that's handing over python's equivalent of something like $Config{prefix}.
      C:\>perl -V:prefix prefix='c:\MinGW\perl524_64int';
      In perl I could easily hack a solution such that perl -V:prefix returned a forward-slashed rendition of that path.
      I was hoping that python might also lend itself to such hacking.

      looking at the _getuserbase() code within the /usr/lib/python2.7/sysconfig.py file, the result may be coming from somewhere in the os.path method (os.py file),

      Hmmm ... altering os.sep might (or might not) be all that's needed.
      How would I change that setting ?
      Is there a simple python command (or script) that will tell me the current setting of os.sep ?

      Cheers,
      Rob
Re: [OT++] Seeking Win32 Python Wisdom
by Anonymous Monk on Mar 16, 2017 at 12:22 UTC
    Isnt there a shell that takes backslashes? I'll have to check my notes later but i think there is
      Isnt there a shell that takes backslashes?

      There's the cmd.exe shell (but that shell is unusable for this exercise).
      Also, the msys2 shell will accommodate backslashes in the path ... so long as they're escaped:
      sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:\\_32\\Python27\\doc python274.chm sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:\\\_32\\\Python27\\\doc python274.chm sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:\\\\_32\\\\Python27\\\\doc python274.chm sisyphus@Owner-PC311012 MINGW64 ~ $ ls C:\\_32\\\Python27\\\\doc python274.chm
      That could (potentially) be useful.

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1184865]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-09-25 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (276 votes). Check out past polls.

    Notices?