- Python, how to get the actual file location
- Python, how to get the actual file location
- How do I find the location of Python module sources?
- How can I find where Python is installed on Windows?
- How to get the python.exe location programmatically? [duplicate]
- How to Find the Location of Python Module Sources
- How do I find the location of Python module sources?
- How to retrieve a module’s path?
- Where are the python modules stored?
- How do I find the location of my Python site-packages directory?
- Practical Tips
- How do I find the location of Python module sources while I can not import it?
- Where are python modules stored?
- is there a way to view the source of a module from within the python console?
- How can I find the location of the source code of a built-in Python method?
- how to get module location
Python, how to get the actual file location
Unless you want to learn and implement the rules (which are documented, but painful, for CPython 2.x, and not documented at all for other implementations, or 3.x) for mapping to files; dealing with .zip archives, eggs, and module packages; trying different ways to get the path to / files that don’t support ; figuring out what Jython/IronPython/PyPy do; etc. Each installed Python version will have a registry key in either: In 64-bit Windows, it will be under the key:
Python, how to get the actual file location
Using python 3: How do I get the path of my files? Please check the code below:
path =r'\\Desktop' os.chdir(path) for root, dirs, files in os.walk(path): for txt_file in files: if txt_file.endswith(".txt"): txt_fileSh=txt_file.rstrip(".txt") path_txt_file=os.path.abspath(txt_file) print(path_txt_file)
\\Desktop\Fig1\a.txt \\Desktop\Fig2\b.txt
Help is very much appreciated.
You don’t need to change directory. You can build the full path based on the root of the walk as follows:
path = r'\Desktop' import os for root, _, files in os.walk(path): for file in files: if file.endswith('.txt'): print(os.path.join(root, file))
The documentation for abspath states that:
os.path.abspath(path) Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
So, you are actually just joining your path with the filename.
You want to use the path to the current directory instead, which is the first of the three items returned by os.walk , the one you named root .
path_txt_file=os.path.abspath(txt_file)
path_txt_file = os.path.join(root, txt_file)
Python, how to get the actual file location, Python, how to get the actual file location. Using python 3: How do I get the path of my files? Please check the code below: path =r’\\Desktop’ os.chdir (path) for root, dirs, files in os.walk (path): for txt_file in files: if txt_file.endswith («.txt»): txt_fileSh=txt_file.rstrip («.txt») path_txt_file=os.path.abspath (txt_file) …
How do I find the location of Python module sources?
How do I learn where the source file for a given Python module is installed? Is the method different on Windows than on Linux?
I’m trying to look for the source of the datetime module in particular, but I’m interested in a more general answer as well.
For a pure python module you can find the source by looking at themodule.__file__ . The datetime module, however, is written in C, and therefore datetime.__file__ points to a .so file (there is no datetime.__file__ on Windows), and therefore, you can’t see the source.
If you download a python source tarball and extract it, the modules’ code can be found in the Modules subdirectory.
For example, if you want to find the datetime code for python 2.6, you can look at
Python-2.6/Modules/datetimemodule.c
You can also find the latest version of this file on github on the web at https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c
Running python -v from the command line should tell you what is being imported and from where. This works for me on Windows and Mac OS X.
C:\>python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python24\lib\site.pyc has bad mtime import site # from C:\Python24\lib\site.py # wrote C:\Python24\lib\site.pyc # C:\Python24\lib\os.pyc has bad mtime import os # from C:\Python24\lib\os.py # wrote C:\Python24\lib\os.pyc import nt # builtin # C:\Python24\lib\ntpath.pyc has bad mtime .
I’m not sure what those bad mtime’s are on my install!
I realize this answer is 4 years late, but the existing answers are misleading people.
The right way to do this is never __file__ , or trying to walk through sys.path and search for yourself, etc. (unless you need to be backward compatible beyond 2.1).
It’s the inspect module—in particular, getfile or getsourcefile .
Unless you want to learn and implement the rules (which are documented, but painful, for CPython 2.x, and not documented at all for other implementations, or 3.x) for mapping .pyc to .py files; dealing with .zip archives, eggs, and module packages; trying different ways to get the path to .so / .pyd files that don’t support __file__ ; figuring out what Jython/IronPython/PyPy do; etc. In which case, go for it.
Meanwhile, every Python version’s source from 2.0+ is available online at http://hg.python.org/cpython/file/X.Y/ (e.g., 2.7 or 3.3). So, once you discover that inspect.getfile(datetime) is a .so or .pyd file like /usr/local/lib/python2.7/lib-dynload/datetime.so , you can look it up inside the Modules directory. Strictly speaking, there’s no way to be sure of which file defines which module, but nearly all of them are either foo.c or foomodule.c , so it shouldn’t be hard to guess that datetimemodule.c is what you want.
If you’re using pip to install your modules, just pip show $module the location is returned.
How to get the python.exe location programmatically?, Note that you can have multiple installs of python, I do on my machine. However, if you install via an msi of a version of python 2.2 or above, I believe it creates a registry key like so: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App …
How can I find where Python is installed on Windows?
I want to find out my Python installation path on Windows. For example:
How can I find where Python is installed?
In your Python interpreter, type the following commands:
>>> import os >>> import sys >>> os.path.dirname(sys.executable) 'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
If you have Python in your environment variable then you can use the following command in cmd or powershell:
If you need to know the installed path under Windows without starting the python interpreter, have a look in the Windows registry.
Each installed Python version will have a registry key in either:
- HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
- HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
In 64-bit Windows, it will be under the Wow6432Node key:
Python logging — check location of log files?, The logging module uses handlers attached to loggers to decide how, where, or even if messages ultimately get stored or displayed. You can configure logging by default to write to a file as well. You should really read the docs, but if you call logging.basicConfig (filename=log_file_name) where log_file_name is the name …
How to get the python.exe location programmatically? [duplicate]
Basically I want to get a handle of the python interpreter so I can pass a script file to execute (from an external application).
This works in Linux & Windows:
>>> import sys >>> print(sys.executable) C:\path\to\python.exe
>>> import sys >>> print sys.executable /usr/bin/python
sys.executable is not reliable if working in an embedded python environment. My suggestions is to deduce it from
I think it depends on how you installed python. Note that you can have multiple installs of python, I do on my machine. However, if you install via an msi of a version of python 2.2 or above, I believe it creates a registry key like so:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
which gives this value on my machine:
You just read the registry key to get the location.
However, you can install python via an xcopy like model that you can have in an arbitrary place, and you just have to know where it is installed.
Print directory where file is saved with Python, 4 Answers. Sorted by: 2. Convert the relative path to absolute path: path = os.path.abspath (filename) Then take just the directory from the absolute path: directory = os.path.dirname (path) That will work regardless of what filename is. It could by just a filename, a relative path or absolute path.
How to Find the Location of Python Module Sources
How do I find the location of Python module sources?
For a pure python module you can find the source by looking at themodule.__file__ .
The datetime module, however, is written in C, and therefore datetime.__file__ points to a .so file (there is no datetime.__file__ on Windows), and therefore, you can’t see the source.
If you download a python source tarball and extract it, the modules’ code can be found in the Modules subdirectory.
For example, if you want to find the datetime code for python 2.6, you can look at
Python-2.6/Modules/datetimemodule.c
You can also find the latest version of this file on github on the web at
https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c
How to retrieve a module’s path?
import a_module
print(a_module.__file__)
Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)
path = os.path.dirname(a_module.__file__)
To get the module’s directory.
Where are the python modules stored?
Usually in /lib/site-packages in your Python folder. (At least, on Windows.)
You can use sys.path to find out what directories are searched for modules.
How do I find the location of my Python site-packages directory?
There are two types of site-packages directories, global and per user.
- Global site-packages («dist-packages») directories are listed in sys.path when you run:
python -c 'import site; print(site.getsitepackages())'
Caution: In virtual environments getsitepackages is not available with older versions of virtualenv , sys.path from above will list the virtualenv’s site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:
python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
Practical Tips
- .__path__ lets you identify the location(s) of a specific package: (details)
$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']
$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.py
$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
How do I find the location of Python module sources while I can not import it?
import imp
imp.find_module('cv2')
Where are python modules stored?
Suppose you want to know the location of xyz module. You do this on the interpreter
You’ll get the location of the pyc file from where your module is being imported.
to get the module name and the location of the module.
To know about all the possible locations from where the modules are imported, use sys.path :
This will give you the list of the locations where Python searches for a module when you do import .
is there a way to view the source of a module from within the python console?
Some of the methods of inspect module are well-suited for this purpose:
import module
import inspect
src = inspect.getsource(module)
How can I find the location of the source code of a built-in Python method?
You can usually find the source files for core python modules in the python installation folder itself. For instance, on linux , I can find the source code for os module which is a quite popular python module in this location:
If you are on windows , this is generally C:\python27\lib , but you can verify it for yourself by running which python in case of linux and where python in case of windows .
how to get module location
It is worth mentioning that packages have __file__ attribute which points to __init__.py , they also have __path__ which points to the package directory. So you can use hasattr(module_name, ‘__path__’) and module_name.__path__[0] or module_name.__file__ .
import socket, SOAPpy # SOAPpy is a package
socket.__file__
# . /python2.5/socket.pyc
socket.__path__
# AttributeError: 'module' object has no attribute '__path__'
SOAPpy.__file__
# . /2.5/site-packages/SOAPpy/__init__.pyc
SOAPpy.__path__
# ['. /2.5/site-packages/SOAPpy']