Python Forum
looking fo documentation for module files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking fo documentation for module files
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
For single or couple .py files can get away to give files then most people know how to run Python script on Linux.
Python find files bye looking in sys.path and OS find Python bye look in environment variables.

The standard way in Python when make a module/package at want to share with other is to use setuptools or eg Poetry(that make it simpler).
Then will first make a wheel the can distribute that wheel on PyPi to reach a bigger audience.
Then pip will be used eg pip install requests this will install files to right place in all OS Windows,Linux and Mac.
The files will go to site-packages or dist-packages(Debian-specific for default Python that comes with OS)

It dos not matter at all where Python is located pip will install to right place.
So is use eg virtual environment this is like new Python version,doing 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.
All files and 3-party modules should be in setup file made bye setuptools
The is only pip install my_module or pip install my_module.whl(if not use PyPi can just give someone the wheel(.whl) file).

So a little overview as this as this can be big topic,it has gotten easier be as Python has taken part this more serious.
You can read my Dark Age and the Broken past into how bad is was before,
there was no standard way of doing this and many home made solution how to share Python files/modules/packages.
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(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 build
Now 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.
Gribouillis likes this post
Reply
#5
i do not want to make this into a package, at least not right now. all i want to do is put a couple of my modules where they are available on my entire system. i put scripts in /usr/local/bin. should i put modules there? they work there, but because that is where the script is read from by python3.8 (or whatever version i have next).

so, i do not want to read about how to make a package. i avoid too much reading and even more watching videos or listening to sound files (other than music) like podcasts. i do get more out of reading, but dislike spending too much time doing that.

my users on my laptop are basically all myself. the main system is a cloud instance i administer with full root access ("fool root access" as it is sometime known). i am not making (very many) things for the world to use. besides me, the users in my small business are more like sales people. they seem to comprehend the command line way of doing things although click on things works easier for them.

my brother is a retired airline pilot. he can be very technical when needed. his explanations of how flying works are usually over my head. our common side interest is weather. i made him be CEO.

i should not need to tell him anything about modules. he just runs some commands or click on some icons. he wanted the weather radar window to come up automatically when he logs in, so i had to do that for him. he can understand technical things, but, computers are something he wants to avoid (they do make flying difficult, according to many pilots).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
First do this
>>> 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.

You could also create a specific directory, for example
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.
Reply
#7
users may code their own scripts, too. whether i code them or they do, the only thing they should have to add is the one import statement that names the module they need.
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(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 command
Output:
python -m skapmod
Reply
#9
I agree with Gribouillis that using site module is good choice that what i have advice many time's(over using PYTHONPATH) for this kind of stuff.
Example answer Thread

PYTHONPATH which is a OS environment variable those content is added to the sys.path where Python looks for modules.
So one advantage that this work all python version as OS environment variable control this.
Make Python look in the project subdirectory of your home directory for modules and packages.
export PYTHONPATH=${PYTHONPATH}:${HOME}/project
So 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', .....]
Reply
#10
(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.

The only case where I use PYTHONPATH is for altering temporarily the environment when I launch a command in the Linux terminal or in a Bash script, for exemple
Output:
$ PYTHONPATH="/some/dir" python spam.py
I don't know if Windows' command line supports this kind of invocation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding which source files import a module Skaperen 3 2,481 Apr-22-2019, 09:28 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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