Python Forum
Find the realpath for a symlinked .pth file in itself with python code.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the realpath for a symlinked .pth file in itself with python code.
#1
Hi,

I symlinked a pth file to the site-packages directory of my virtualenv. Then I want to obtain the realpath of this pth file from within it with python code.

But it seems the python code used in this file must only be one-line python code and it doesn't know the meaning of 'os.path.realpath(__file__)'

Any hints for this problem?

Regards
Reply
#2
import os

os.chdir(os.path.abspath(os.path.dirname(__file__))) # Make sure starting dir is OK

print(os.path.realpath('CreateDict.py'))
Reply
#3
This doesn't solve my problem, I want to obtain the directory from where the pth file is symlinked to the site-packages directory, instead of the site-packages directory itself. Say, if I symlinked the the file with

ln -s ~/test.pth  /path/to/site-packages/ 
And there are the following the lines in this pth file:

import os; os.chdir(os.path.abspath(os.path.dirname(__file__))); print(os.path.realpath('CreateDict.py')) 
This will print the following twice:

$ python
/home/werner/.pyenv/versions/3.8.1/lib/python3.8/CreateDict.py
/home/werner/.pyenv/versions/3.8.1/lib/python3.8/CreateDict.py
Python 3.8.1 (default, Mar 11 2020, 09:08:36) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
But I want the obtain the directory of $HOME.

Regards
Reply
#4
from os.path import expanduser
home = expanduser("~")
Reply
#5
@anbu23 I just use HOME as a simple example, in fact the pth file is not in that directory.

$ ln -s /path/to/test.pth /path/to/site-packages/

In this general case, I want to obtain the source path of the pth file use python code from within it.

Regards
Reply
#6
print(os.path.abspath('test.pth'))
Reply
#7
No, it cannot do the trick.
Reply
#8
You cannot use __file__. It will always return the current script name, as will sys.argv[0]
you must use actual file name:
import os
import sys


def show_symlink():
    # Set current path to that of script
    os.chdir(os.path.abspath(os.path.dirname(__file__)))

    # get address of self
    print(f"value of __file__: {__file__}")
    print(f"value of sys.argv[0]: {sys.argv[0]}")

    print(f"Current working directory: {os.getcwd()}")

    print(f"using realpath: {os.path.realpath('CreateDict.py')}")
    print(f"using readlink: {os.readlink('CreateDict.py')}")

show_symlink()
will give:
Output:
value of __file__: .../projects/T-Z/T/Trailmapper/Florida2020/src/TestArea/ShowSympath.py value of sys.argv[0]: .../projects/T-Z/T/Trailmapper/Florida2020/src/TestArea/ShowSympath.py Current working directory: .../projects/T-Z/T/Trailmapper/Florida2020/src/TestArea using realpath: .../projects/T-Z/T/Trailmapper/src/CreateDict.py using readlink: ../CreateDict.py
As you can see, print(f"using realpath: {os.path.realpath('CreateDict.py')}") gives the full directory of what the symlink points to
As does print(f"using readlink: {os.readlink('CreateDict.py')}") albeit that this is a relative path (exactly as originally defined with the ln command)

CreateDict.py resides one directory above TestArea (in the src Directiry), the symlink is in TestArea

This works properly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,031 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,558 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Cannot find py credentials file standenman 5 1,629 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 737 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Find (each) element from a list in a file tester_V 3 1,204 Nov-15-2022, 08:40 PM
Last Post: tester_V
  what will be the best way to find data in txt file? korenron 2 1,150 Jul-25-2022, 10:03 AM
Last Post: korenron
  find some word in text list file and a bit change to them RolanRoll 3 1,518 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Problem with importing Python file in Visual Studio Code DXav 7 5,064 Jun-15-2022, 12:54 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 3,452 Mar-18-2022, 07:49 PM
Last Post: snippsat
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,395 Feb-13-2022, 07:13 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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