위챗 애플릿 미니 프로그램 개발 Python 사용자 정의 모듈 및 패키지에 대한 자세한 설명

Python 사용자 정의 모듈 및 패키지에 대한 자세한 설명

Mar 16, 2017 pm 02:19 PM

1、保存一个hello.py文件在F:/data/python目录下

  • hello.py

>>> def hello(x):    print x
로그인 후 복사

  • 目录

详解python自定义模块、包

  • 导入

>>> import sys>>> sys.path.append('F:/data/python') #添加路径,此添加是一次性的>>> import hello>>> hello.hello(5)  #调用5
로그인 후 복사

 2、不改变sys.path导入自定义模块:

第一种方法:将hello2.py文件放在D:/Python27/lib/site-packages目录下

>>> import sys,pprint>>> pprint.pprint(sys.path)
['', 'D:\\Python27\\Lib\\idlelib', 'F:\\data\\s', 'D:\\Python27', 'C:\\Windows\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27\\lib\\site-packages']>>> import hello2>>> hello2.hello()
hello,world!
로그인 후 복사

 第二种方法:将F:/data/python路径添加到环境变量(在系统变量中,添加PYTHONPATH变量,再将路径添加到值中)

>>> import sys>>> import hello>>> hello.hello(1)1
로그인 후 복사

 3、自定义包,导入模块

首先,创建如下目录,pytest为包,将路径F:\test添加到环境变量PYTHONPATH中,pytest包中init.py文件是必要的,内容可以为空

详解python自定义模块、包

>>> import sys>>> from pytest import hello>>> hello.hello(4)4
로그인 후 복사

 

위 내용은 Python 사용자 정의 모듈 및 패키지에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)