Is it a bug of os.path.abspath to translate '~/path'? - python

See below:
$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.abspath('~/gitlab')
'/home/swift/gitlab/swift_test_code/swift/core/~/gitlab'
>>> os.path.expanduser('~/gitlab')
'/home/swift/gitlab'
Does anyone know why abspath doesn't expand ~ as the home directory? It's tricky to call expand before calling abspath.

Related

Why did the python version not switch after conda switched the environment?

python3.7.12 should be the python version of the hsm environment
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ which python
/home/leelee/miniconda3/envs/hsm/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ /home/leelee/miniconda3/envs/hsm/bin/python
Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:21)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
But if I type python directly, it will return a 3.9.5 python version
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ python
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
I guess when I type python, I entered /home/leelee/miniconda3/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ which -a python
/home/leelee/miniconda3/envs/hsm/bin/python
/home/leelee/miniconda3/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ /home/leelee/miniconda3/bin/python
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
Why does this happen and how can I solve it?
Looks like you added Python to the system PATH, which is not recommended when using conda. Simply check your PATH after conda activation and have a look at the sequence of the Python folders.

Python logging module platform differences: OSX vs. Linux

Can anyone help me understand why python's logging module creates a StreamHandler on import, but only on some distributions? weird.. (note: happens on any python3)
On Mac
Python 3.5.6 (default, Dec 19 2019, 14:59:39)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.getLogger().handlers
[<logging.StreamHandler object at 0x108fc2630>]
Linux:
Python 3.7.4 (default, Jan 3 2020, 19:27:19)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.getLogger().handlers
[]
>>>
I can't seem to find any explanation about this behavior difference!
EDIT:
A bit more investigation suggests that some module installed in the virtualenv is causing this behavior mismatch. I created a fresh one with the same python interpreter and no handler gets made. I'm trying to investigate which module seems to cause this.
This isn't actually a platform difference, but was a result of an installed library.
I filed the issue upstream https://github.com/stuaxo/vext/issues/63

How to edit python path

I use both python3.5.3 (default) and python 3.6.5 that I installed on raspberry pi.
The path for each version is as below.
I'd like to add the path, /usr/lib/python3/dist-packages, under python3.6 and remove it from python3.5. How could I do that?
user#raspberrypi:~ $ python3.6
Python 3.6.5 (default, Apr 5 2018, 18:01:08)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
'/usr/local/lib/python36.zip',
'/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages']
user#raspberrypi:~ $ python3.5
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-arm-linux-gnueabihf',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
As each version of Python seems to maintain its own environment (and path) how's about adding this to the beginning of your program:
import sys
myPath='/usr/lib/python3/dist-packages' # or whatever else you want it to be
if '3.6' in sys.version and not myPath in sys.path:
sys.path.append(myPath)
elif '3.5' in sys.version and myPath in sys.path:
sys.path.remove(myPath)

Difference between enum behavior with Python

Here's Python 2.7.6 on my Ubuntu laptop:
(myenv)$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class Fruit(enum.Enum):
... apple=1
... orange=2
...
>>> Fruit.apple
<Fruit.apple: 1>
>>> type(Fruit.apple)
<enum 'Fruit'>
and here's it on Heroku (also 2.7.6):
~ $ python
Python 2.7.6 (default, Jul 15 2014, 15:38:10)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class Fruit(enum.Enum):
... apple=1
... orange=2
...
>>> Fruit.apple
1
>>> type(Fruit.apple)
<type 'int'>
EDIT: My requirements.txt file contains this line:
enum==0.4.4
This is legitimately causing issues as my production version exhibits bugs the local version does not!
Looks like your ubuntu version is the enum34 backport, while the heroku version is the older enum package.
If I read your question correctly and the version with enum34 is working properly, change your requirements file to use it instead.
Check the versions of enum library. In your env there is probably older and deprecated version. Anyway field inside enum class must be of its declared type, which in your case is int.
... apple=1
... orange=2
and type of
fruit = Fruit
would be Fruit
$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> enum.__version__
'0.4.4'
Try checking enum.__version__.

libsvm on linux - how to add path

I downloaded the tar file for libsvm, navigated to the python directory and ran the make command. This words and when I run python inside that same directory,
import svm
works just fine. But not in any other directory. What can I do to make this library accessible from any where? I know it has some thing to do with copying the path some where, but not sure since I'm a newbie at linux.
what linux distro are you on? on my Ubunto I just:
$ sudo apt-get install python-libsvm
...
$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svm
>>>
$ cd /tmp
$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svm
>>>

Categories