https://www.youtube.com/watch?v=AM2dgUAdwaQ
once
====
sudo apt install python3.8-venv
library
=======
mkdir callib
touch callib/__init__.py
touch callib/tools.py
echo 'def add_them(x, y):' >> callib/tools.py
echo ' print(f"adding {x} and {y}")' >> callib/tools.py
echo ' return x+y' >> callib/tools.py
touch setup.py
echo 'import setuptools' >> setup.py
echo 'setuptools.setup(name="callib", version="1.0", packages=["callib"])' >> setup.py
build and test
==============
python3 -m venv venv
source venv/bin/activate
pip install wheel setuptools
python setup.py bdist_wheel --universal
cd dist
ls callib-1.0-py2.py3-none-any.whl
python3 -m venv venv
source venv/bin/activate
pip install callib-1.0-py2.py3-none-any.whl
touch test.py
echo 'from callib import tools' >> test.py
echo 'print(tools.add_them(2,3))' >> test.py
python3 test.py
echo "now we should see adding 2 and 3"
multiple modules
================
mkdir f20001
touch f20001/__init__.py
touch f20001/__main__.py
nano f20001/__main__.py
```
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, 20001!</p>"
if __name__=="__main__":
app.run(host='0.0.0.0', port=20001)
```
mkdir f20002
touch f20002/__init__.py
touch f20002/__main__.py
nano f20002/__main__.py
```
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, 20002!</p>"
if __name__=="__main__":
app.run(host='0.0.0.0', port=20002)
```
mkdir src/
mkdir src/f20003
touch src/f20003/__init__.py
touch src/f20003/__main__.py
nano src/f20003/__main__.py
```
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, 20003!</p>"
if __name__=="__main__":
app.run(host='0.0.0.0', port=20003)
```
setup.py for multiple modules
======================
touch setup.py
cat /dev/null > setup.py
nano setup.py
```
import setuptools
setuptools.setup(name="callib", version="1.0", package_dir={"f20003":"src/f20003"}, packages=["callib", "f20001", "f20002", "f20003"], install_requires=["flask"])
```
build and test for multiple modules
====================
source venv/bin/activate
rm -rf dist
python setup.py bdist_wheel --universal
cd dist
python3 -m venv venv
source venv/bin/activate
echo "pip install callib-1.0-py2.py3-none-any.whl"
pip install *.whl
python3 -m f20001
firefox http://127.0.0.1:20001
python3 -m f20002
firefox http://127.0.0.1:20002
python3 -m f20003
firefox http://127.0.0.1:20003
沒有留言:
張貼留言