1、保存一个hello.py文件在F:/data/python目录下
hello.py
>>> def hello(x): print x
目录
导入
>>> 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文件是必要的,内容可以为空
>>> import sys>>> from pytest import hello>>> hello.hello(4)4
The above is the detailed content of Detailed explanation of python custom modules and packages. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor