(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.It's not ok to go back to old days where users had to copy files around and also manually install 3-party libraries needed.
To give a simple example.
Folders/files setup:
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
The new way is to use pyproject.toml, the old way was to use
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).Then user can use it like this with
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
On step more is to upload wheel file to PyPi if want to share it with many.
Then the install would be on PyPi
pip install webtitle
available for all that use pip.