Jul-24-2022, 10:25 PM
i'm looking for documentation that covers where to store a file to make it generally available to all Python scripts run in a typical way by any user on a Linux or Unix system.
.py
files can get away to give files then most people know how to run Python script on Linux.sys.path
and OS find Python bye look in environment variables
.pip install requests
this will install files to right place in all OS Windows,Linux and Mac.site-packages
or dist-packages
(Debian-specific for default Python that comes with OS)pip
will install to right place.pip install requests
will install all new files to this new Python version.Never
should eg tell users that they have to place .py
files in a specific folder for my module/package to work.pip install my_module
or pip install my_module.whl
(if not use PyPi can just give someone the wheel(.whl)
file).(Jul-27-2022, 01:48 AM)Skaperen Wrote: [ -> ]isn't there just a simple "copy the file(s) to this path ..." that will make that module generally available to be imported. what i have been doing was putting the modules in /usr/local/bin with the scripts that use them. but mixing them like that just seems wrong.You can personally do what work for you,but it want to share and other to use code then should build it the standard way so users can use
pip
to install and also uninstall.project\ web_prog\ |-- find_title.py
# find_title.py import requests from bs4 import BeautifulSoup def web_title(url): '''Find web page title''' url_get = requests.get(url) soup = BeautifulSoup(url_get.content, 'lxml') print(soup.select('head > title')[0].text) if __name__ == '__main__': url = 'https://www.python.org/' web_title(url)Now i want to share this code so gone make a
wheel(.whl)
and also tar.gz
setup.py
.[build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" [project] name = "Webtitle" version = "0.1" description = "Title_find" requires-python = ">=3.7" classifiers = [ "Programming Language :: Python :: 3", ] dependencies = [ "requests", "bs4", "lxml" ] [tool.setuptools] packages = ["web_prog"]Now gone build it.
pip install -q build python -m buildNow have two files:
Output:Webtitle-0.1-py3-none-any.whl
Webtitle-0.1.tar.gz
So if i want to share this can give Webtitle-0.1-py3-none-any.whl
to user(this will work on Windows, Linux and Mac).pip
.pip install Webtitle-0.1-py3-none-any.whl Processing g:\div_code\project_env\webtitle-0.1-py3-none-any.whl Collecting lxml ..... Installing collected packages: urllib3, soupsieve, lxml, idna, charset-normalizer, certifi, requests, beautifulsoup4, bs4, Webtitle Running setup.py install for bs4 ... done Successfully installed Webtitle-0.1 beautifulsoup4-4.11.1 bs4-0.0.1 certifi-2022.6.15 charset-normalizer-2.1.0 idna-3.3 lxml-4.9.1 requests-2.28.1 soupsieve-2.3.2.post1 urllib3-1.26.11
Teste that it work. G:\div_code\ λ python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from web_prog import find_title >>> >>> find_title.web_title('https://cnn.com') CNN International - Breaking News, US News, World News and Video >>> exit()If user don't want code anymore can
uninstall
with pip
.G:\div_code λ pip uninstall Webtitle-0.1-py3-none-any.whl Found existing installation: Webtitle 0.1 Uninstalling Webtitle-0.1: Would remove: c:\python310\lib\site-packages\web_prog\find_title.py c:\python310\lib\site-packages\webtitle-0.1.dist-info\* Proceed (Y/n)? y Successfully uninstalled Webtitle-0.1
pip install webtitle
available for all that use pip.>>> import site >>> site.getsitepackages() ['/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.8/dist-packages']You can store your python modules in one of the directories on the returned list to make them available to all users.
Output:mkdir /usr/local/lib/python3.8/our-site-packages
Then>>> import sitecustomize >>> sitecustomize <module 'sitecustomize' from '/usr/lib/python3.8/sitecustomize.py'>Edit sitecustomize.py and add this
import site site.addsitedir('/usr/local/lib/python3.8/our-site-packages')Then use this directory for your modules.
from skapmod import *i know i can create a directory just about anywhere and instruct them what to do to use it. but i just want to use a directory that Python already searches in, without running Python under strace to see what all it really does (there may be times to do this but now is not one of them).
(Jul-29-2022, 11:39 PM)Skaperen Wrote: [ -> ]users may code their own scripts, too.Users can use the directory returned by
site.getusersitepackages()
. Note that all these directories are directories for python modules, not executable scripts. Of course when the module skapmod exists, it can be used as a script with the commandOutput:python -m skapmod
site module
is good choice that what i have advice many time's(over using PYTHONPATH
) for this kind of stuff.PYTHONPATH
which is a OS environment variable those content is added to the sys.path
where Python looks for modules.project
subdirectory of your home
directory for modules and packages. export PYTHONPATH=${PYTHONPATH}:${HOME}/projectSo on Windows i have
div_code
and project
folder added to PYTHONPATH
because i store .py
files and work mostly for those folder.G:\div_code λ python -V Python 3.10.5 G:\div_code λ python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> >>> sys.path ['', 'G:\\Projects', 'G:\\div_code', 'C:\\python310', .....]If i go back to older version the folder is always there as OS control this.
G:\div_code λ py -3.7 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> >>> sys.path ['', 'G:\\Projects', 'G:\\div_code', 'C:\\Python37', .....]
(Jul-30-2022, 09:53 AM)snippsat Wrote: [ -> ]So one advantage that this work all python version as OS environment variable control this.This is also the greatest flaw of PYTHONPATH. Using the same installed modules for several versions of Python leads to troubles and inconsistencies.
Output:$ PYTHONPATH="/some/dir" python spam.py
I don't know if Windows' command line supports this kind of invocation.