Same folder
If the module is in your working directory, importlib.util.find_spec probably suffices for your purposes.
For example if you just want to load the module, you can use:
deprecated in Python 3.5 and higher:
toolbox_specs = importlib.util.find_spec("Tools") toolbox = toolbox_specs.loader.load_module()introduced in Python 3.5 and higher:
toolbox_specs = importlib.util.find_spec("Tools") toolbox = importlib.util.module_from_spec(toolbox_specs) toolbox_specs.loader.exec_module(toolbox)Caveat: I haven’t tested this, but it’s straight from the documentation, so I suppose it works.
You can assess several other properties with the toolbox_specs object.
However, e.g., a corresponding file object is not amongst them.
If you really need this in Python 3, you probably have to obtain the file’s path and open it with other methods.
Different folder
To find a module in a different folder, you have to work with a FileFinder, which in turn needs to know the module’s type. For example, if your module is an extension, you can find the specs as follows:
loader_details = (
importlib.machinery.ExtensionFileLoader,
importlib.machinery.EXTENSION_SUFFIXES
)
toolsfinder = importlib.machinery.FileFinder("Folder_of_Tools", loader_details)
toolbox_specs = toolsfinder.find_spec("Tools")
You can then process toolbox_specs as described above.
Same folder
If the module is in your working directory, importlib.util.find_spec probably suffices for your purposes.
For example if you just want to load the module, you can use:
deprecated in Python 3.5 and higher:
toolbox_specs = importlib.util.find_spec("Tools") toolbox = toolbox_specs.loader.load_module()introduced in Python 3.5 and higher:
toolbox_specs = importlib.util.find_spec("Tools") toolbox = importlib.util.module_from_spec(toolbox_specs) toolbox_specs.loader.exec_module(toolbox)Caveat: I haven’t tested this, but it’s straight from the documentation, so I suppose it works.
You can assess several other properties with the toolbox_specs object.
However, e.g., a corresponding file object is not amongst them.
If you really need this in Python 3, you probably have to obtain the file’s path and open it with other methods.
Different folder
To find a module in a different folder, you have to work with a FileFinder, which in turn needs to know the module’s type. For example, if your module is an extension, you can find the specs as follows:
loader_details = (
importlib.machinery.ExtensionFileLoader,
importlib.machinery.EXTENSION_SUFFIXES
)
toolsfinder = importlib.machinery.FileFinder("Folder_of_Tools", loader_details)
toolbox_specs = toolsfinder.find_spec("Tools")
You can then process toolbox_specs as described above.
I found this worked as a replacement for imp.find_module()
importlib.machinery.PathFinder().find_spec(mod_name, module_dir_list)
Replace uses of imp.find_module / imp.load_module with importlib in the importer
How do I find the location of Python module sources? - Stack Overflow
path - Python can't find my module - Stack Overflow
How to find where my Python packages are going?
Videos
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!
Essentially, when you execute script.py directly, it doesn't know that it's part of a submodule of src, nor does it know where a module named src might be. This is the case in either python 2 or 3.
As you know, Python finds modules based on the contents of sys.path. In order to import any module, it must either be located in a directory that's listed in sys.path, or, in the same directory as the script that you're running.
When you say python src/scripts/script.py, sys.path includes the Project/src/scripts/ (because that's where script.py is located), but not Project. Because Project isn't in the path, the modules in that directory (src) aren't able to be imported.
To fix this:
I'm assuming that your script.py is an entry point for your src module (for example, maybe it's the main program). If that's true, then you could fix it by moving script.py up to the same level as src:
Project
├───.git
├───venv
|───script.py <--- script.py moves up here
└───src
├───__init__.py
└───mymodules
├───__init__.py
├───module1.py
└───module2.py
This way, script.py can freely import anything in src, but nothing in src can import script.py.
If that's not the case, and script.py really is a part of src, you can use python's -m argument to execute script.py as part of the src module like so:
$ python -m src.scripts.script
Because you've told python which module you're running (src), it will be in the path. So, script.py will be aware that it's a submodule of src, and then will be able to import from src.
Be careful in this situation though - there's potential to create a circular import if something in src imports src.scripts.script.
As an alternative to both of these approaches, you can modify the sys.path directly in script.py:
import sys
sys.path.insert(0, '/path/to/Project') # location of src
While this works, it's not usually my preference. It requires script.py to know exactly how your code is laid out, and may cause import confusion if another python program ever tries to import script.py.
Project
├───.git
├───venv
└───src
├───__init__.py
├───mymodules
│ ├───__init__.py
│ ├───module1.py
│ └───module2.py
└───scripts
├───__init__.py
└───script.py
Alternatively you can import like the following in your script.py
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__),'../../'))
import src.mymodules.module1
Now you can run script.py file from any location.
e.g :
python script.py
python /path to folder/script.py
I did `pip list` and saw a list of packages. I wondered where they were coming from. I found a list of many of them in "/Users/myname/opt/anaconda3/pkgs". To check if that is where they go, I did `pip install python-math`. I did `pip list` again and saw python-math was in the list output in the terminal. I went to "/Users/myname/opt/anaconda3/pkgs" and python-math wasn't there. I did a search for "python-math" and couldn't find anything.
I would really like to have a logical understanding of where things go and what program puts them there and how to configure the program that puts them there. Any help is appreciated. I'm on a Mac M1 2020. I use VSCode. I have Xcode. Thank you.
I'm trying to import/use an ETL package, PETL, withimport petl as etl
I did
pip install petl
from my project's root library; it appeared to install fine but VSCode is telling me it can't find the module; and when I try to run it from the terminal there it gives syntax error
I'm using python 3.7; I don't think I have multiple versions of python installed, but not sure how to check that
this seems to be a relatively common problem; a quick google answer says it's not looking where pip installed it
6 months ago I installed pymssql, am using that in the same script, and am able to find that fine (I'm just now getting back into a small python project)
says it's not looking where pip installed it
If you run "pip -V" in the cli it will display where pip will install.
If you run 'import sysconfig; print(sysconfig.get_paths()["purelib"])' it will show where python looks for packages.
If you know which interpreter you want to use, you can ensure an install will be going to the right place by running "python3 -m pip install mymodule" .
I know this has already been answered, but just to throw it out here:
You can also type "pip list -v" in CMD to list locations of all installed packages