目录


一、jupyter notebook/jupyterlab

Anaconda完整版已经默认安装,Miniconda没有安装。

  • 安装
conda install jupyter notebook
# pip uninstall jupyter
pip install jupyter
  • 1.
  • 2.
  • 3.
  • 运行
jupyter notebook
  • 1.

浏览器地址栏中默认地将会显示: http://localhost:8888

  • 指定端口启动
jupyter notebook --port 9999
  • 1.
  • 修改主目录
    默认jupyter notebook所编写的文档是默认当前的家目录,或者启动命令的目录(在该目录中cmd并激活虚拟环境,然后再启动jupyter notebook)。
    如果想要修改默认文件存储路径,那需要修改jupyter Notebook的文件存放路径。
# 生成配置文件
jupyter notebook --generate-config
  • 1.
  • 2.

常规的情况下,Windows和Linux的配置文件所在路径在家目录下的.jupyter下,配置文件名:jupyter_notebook_config.py,修改如下:

c.NotebookApp.notebook_dir
  • 1.
  • 安装jupyterlab
pip install jupyterlab
conda install -c anaconda jupyter
  • 1.
  • 2.
  • 启动jupyterlab
jupyter-lab
或者
jupyter lab
  • 1.
  • 2.
  • 3.

jupyterlab与jupyter notebook配置文件和默认工作目录相同。

jupyter notebook和jupyterlab都使用ipython作为处理内核(ipykernel)

二、生成wheel并上传PyPI

# 安装wheel和twine包:
pip install wheel twine

# 在命令行中,转到您的Python项目目录——就是setup.py所在的目录,之后的命令都是在该目录中执行。
# ‌Python中的__init__.py文件主要用于将目录变为一个Python包,并定义包的行为。 它通常包含初始化代码,用于设置包的状态或执行必要的设置操作。此外,__init__.py文件还可以用来控制包的导入行为,指定哪些模块或对象在导入包时被自动导入。‌

project
   ├──function
   |    ├──__init__.py
   |    ├──img_slice.py
   |    ├──utils
   |         ├──__init__.py
   ├──setup.py

# 使用setup.py文件生成wheel文件。
# 运行完成后,会在project目录下生成两个文件夹,一个build文件夹,一个dist文件夹,生成的wheel就在dist文件夹中。
python setup.py bdist_wheel

# 安装wheel文件(*.whl使用zip来压缩)
pip install prj_name-0.0.1-py3-none-any.whl
pip install -U prj_name-0.0.1-py3-none-any.whl # 升级安装

# 本地测试(一个版本号只能上传一次)
python setup.py develop

# 本地编译——生成tar.gz的压缩包
python setup.py sdist

# 上传wheel文件到PyPI(The Python Package Index)
twine upload dist/*
twine upload dist/prj_name-0.0.1-py3-none-any.whl
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

确保您已经在~/.pypirc文件中配置了PyPI的访问凭据,或者在执行twine upload命令时通过命令行提示输入用户名和密码。

这里是一个简单的setup.py示例:

from setuptools import setup, find_packages
 
setup(
    name='your_package_name',
    version='0.1',
    description='Your package short description',
    long_description='''A longer description of your package that can span multiple lines''',
    author='Your Name',
    author_email='your.email@example.com',
    url='http://your.package.home.page',
    packages=find_packages(),  # Include all packages under your project
    install_requires=[
        # List of dependencies of your package
    ],
    classifiers=[
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
    ],
)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

替换其中的参数为您自己的信息。然后在命令行中执行上述步骤即可生成并上传wheel文件。
setup.py的另一个示例

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="sigma_t",  # 项目名称
    version="0.0.1",  # 项目版本信息
    author="AlanRick",  # 作者  写你的真实姓名即可
    author_email="zs13128488417@gmail.com",  # 作者邮箱
    description="six sigma project",  # 项目简介
    long_description=long_description,  # 项目详细的介绍  这里直接读取README.md文件
    long_description_content_type="text/markdown",  # 项目详细介绍的文件类型
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir={"": "src"},  # 自己的包所在目录
    packages=setuptools.find_packages(where="src"),  # 所有模块所在目录
    python_requires=">=3.6",  # python所需要的版本
)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

再来一个setup.py的例子

import codecs
import os
from setuptools import setup, find_packages

# these things are needed for the README.md show on pypi (if you dont need delete it)
here = os.path.abspath(os.path.dirname(__file__))

with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
    long_description = "\n" + fh.read()

# you need to change all these
VERSION = '1.0.2'
DESCRIPTION = 'a ligh weight menu , support both win and mac '
LONG_DESCRIPTION = 'dumb_menu is a ligh weight menu ,support hot key, support both win and mac'

setup(
    name="dumb_menu",
    version=VERSION,
    author="clever chen",
    author_email="",
    description=DESCRIPTION,
    long_description_content_type="text/markdown",
    long_description=long_description,
    packages=find_packages(),
    install_requires=[],
    keywords=['python', 'menu', 'dumb_menu','windows','mac','linux'],
    classifiers=[
        "Development Status :: 1 - Planning",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
    ]
)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

参考: https://python-packaging-zh.readthedocs.io/zh-cn/latest/minimal.html#pypi
PyPI的官网: https://pypi.org/

三、VSCode格式化python代码

可以使用Black Formatter插件,然后shit + alt + f

四、关于__name__的解读

第一个执行的模块即程序的入口——“main”。因此python会为第一个执行模块的__name__变量赋值“_main_”字符串。根据该字符串就可以判断程序的入口。

# person.py
class Person:
    def __init__(self, name="joy"):
        self.name = name
        print(f"{self.name} is driver")


print(f"**** {__name__} ****")

#######################################################

# car.py
from person import *


class Car:
    def __init__(self, aPerson):
        self.person = aPerson

    def drive(self):
        print(f"{self.person.name} are driving.")


print(f"==== {__name__} ====")
driver = Person("Abner")
play = Car(driver)
play.drive()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

程序的输出

**** person ****
==== __main__ ====
Abner is driver   
Abner are driving.
  • 1.
  • 2.
  • 3.
  • 4.

从输出可以看到,第一个执行的模块__name__的值是“_main_”,而第二个执行的模块__name__的值就是模块的名字“person”。所以通过__name__变量的值可以判断程序的入口。

if __name__ == "__main__":
    driver = Person("Abner")
    play = Car(driver)
    play.drive()
  • 1.
  • 2.
  • 3.
  • 4.

这里对__name__的判断即可视为C语言中的main()函数——程序的入口点。