理解 python 中的 import

import 会执行 被 import 文件的代码

我们先构建个例子
[gateman@manjaro-x13 python_common_import]$ tree
.
├── pytest.ini
├── README.md
├── requirement.txt
├── src
│   └── import_base
│       ├── __init__.py
│       ├── m1.py
│       └── run1.py
└── test
    └── test_1.py

如上, 新建了1个项目, 在 src/import_base/ 里有两个文件, m1.py 和 run1.py

m1.py

import os

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f1(from_file=filename):
    print("f1 in {} called from {}".format(filename, from_file))


f1()

run1.py

import os
import m1

filename = os.path.basename(__file__)
m1.f1(filename)

可以看出, run1.py 调用里 m1.py的f1()方法,
Note: _file_ 会 return 当前文件的full path

执行测试 m1.py
(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/m1.py 
it's in m1.py
f1 in m1.py called from m1.py

输出正常

执行测试 run1.py
(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
f1 in m1.py called from m1.py
f1 in m1.py called from run1.py

这时, 发现除了run1 调用 m1.f1()的输出外, 之前还包含了m1.py 自己本身的输出!
所以import m1
实际上会执行m1.py 里所有的代码




多次import 同1个模块不会重复执行该模块的代码.

例如我把 run1.py改成

import os
import m1
import m1

filename = os.path.basename(__file__)
m1.f1(filename)

注意这里import了两次m1
但是实际输出还是只会执行1次m1的代码

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
f1 in m1.py called from m1.py
f1 in m1.py called from run1.py

所以python 有1个机制, 当某个模块已经被import时, 再次import 并不会重复执行。




import x 和 from x import 的区别

我修改以下m1.py 和 run1.py
m1.py


filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f1(from_file=filename):
    print("f1 in {} called from {}".format(filename, from_file))

def f2(from_file=filename):
    print("f2 in {} called from {}".format(filename, from_file))

f1()
f2()

run1.py

import os
import m1

filename = os.path.basename(__file__)
m1.f1(filename)
m1.f2(filename)

无非增加了1个函数调用f2()
当执行run1.py时, 输出如下:

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
f1 in m1.py called from m1.py
f2 in m1.py called from m1.py
f1 in m1.py called from run1.py
f2 in m1.py called from run1.py

f1 和 f2 都被调用了




由dir() 决定能不能直接调用某个对象

这次我修改以下run1.py
run1.py

import os
import m1

filename = os.path.basename(__file__)
m1.f1(filename)
f2(filename)

注意f2前面没有m1. prefix
测试时会有error

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
f1 in m1.py called from m1.py
f2 in m1.py called from m1.py
f1 in m1.py called from run1.py
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/import_base/run1.py", line 6, in <module>
    f2(filename)
    ^^
NameError: name 'f2' is not defined

原因很简单, f2 没有定义, 而m1是定义过的。
查看一个模块本身哪些对象已经定义可以 用 dir()方法
再次修改run1.py

import os
import m1

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))
m1.f1(filename)
f2(filename)

加了一行dir()的输出
测试结果:

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
f1 in m1.py called from m1.py
f2 in m1.py called from m1.py
defined objects list: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'filename', 'm1', 'os']
f1 in m1.py called from run1.py
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/import_base/run1.py", line 7, in <module>
    f2(filename)
    ^^
NameError: name 'f2' is not defined

可以见到 dir()里面的对象除了一些built-in的东西
annotations’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec
之外
还有, filename, m1, os 3个
其中m1 和 os 都是import 进来的, filename 是在文件内定义的, 所以都能被调用。
而f2不在其中.
所以 为何m1.f2() 能调用, 直接f2()会出错




from x import 写法能直接调用函数/对象名字, 而不需要带模块名

再修改一次run1.py

import os
from m1 import f2

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

f2(filename)
m1.f1(filename)

这次我们把import m1 改成 from m1 import f2

结果很明显, f2能直接被调用了 但是m1.f1()失败, 因为m1 并不在dir()里面, 而且直接调用f1()也是不行的, 因为只从m1里引用了f2()
而且from m1 import f2() 看来只引用了f2, 但是实际上还是会执行 m1的全部代码的.

.venv) [gateman@manjaro-x13 python_common_import]$ /home/gateman/Projects/python/python_common_import/.venv/bin/python /home/gateman/Projects/python/python_common_import/src/import_base/run1.py
it's in m1.py
f1 in m1.py called from m1.py
f2 in m1.py called from m1.py
defined objects list: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'f2', 'filename', 'os']
f2 in m1.py called from run1.py
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/import_base/run1.py", line 8, in <module>
    m1.f1(filename)
    ^^
NameError: name 'm1' is not defined




from x import 可以从模块x同时引入多个对象or 函数

下面改法也是可以的
run1.py

import os
from m1 import f1,f2

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

f2(filename)
f1(filename)

这时f1 和 f2都能直接调用

import os
from m1 import f1,f2

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

f2(filename)
f1(filename)

注意, 把from m1 import f1, f2 改成两行也是可以的

from m1 import f1
from m1 import f2

上面提过了, 重复引入m1 并不会重复执行m1内的代码




from x import 和 import x 可以同时使用

再次修改run1.py

import os
from m1 import f1
from m1 import f2
import m1

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

f2(filename)
f1(filename)
m1.f1(filename)

这次皆大欢喜, 怎么调用都而可以, 因为m1, f1,f2都在dir()里了

import os
from m1 import f1
from m1 import f2
import m1

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

f2(filename)
f1(filename)
m1.f1(filename)




总结from x import 和 import x 的区别和使用场景
  1. import x
    如果想引用x里的所有的对象, 就直接用import x
    调用x里的对象时必须带上x作为前缀, 例如x.f1()

  2. from x import f1
    如果执行引入x里的1个或者部分对象, 则用from x import f1会更节省资源.
    掉用f1时直接调用, 不能带x 作为前缀




如何避免 m1 里的函数重复调用, _name_ 的使用

上面的例子中, 当run1.py 被执行时, m1的 f1() 被执行了两次, 一次是被import m1 时执行的, 另一次时run1.py 直接调用

import os
import m1 # m1.f1 is called 1st time

filename = os.path.basename(__file__)
print("defined objects list: {}".format(dir()))

m1.f1(filename) # m1.f1 is called 2nd time

如何令 import m1 时, m1 的f1 不被执行? 这个更符合我们代码的习惯
很简单, 在m1 里只保留 f1 定义的代码, 注释掉调用的代码就好

m1.py

import os

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f1(from_file=filename):
    print("f1 in {} called from {}".format(filename, from_file))

def f2(from_file=filename):
    print("f2 in {} called from {}".format(filename, from_file))

# f1() commented out to avoid having it called twice
# f2()

但这样的话, 我们就不能直接执行m1.py 这个文件直接测试 f1了。

首先, 这不是个大问题, 当我们编写java 的工具类时, 通常也不能直接execute 对应的java file的
如果想直接测试, 我们必须在java 工具类添加 main() 函数

同样地, python 有类似的功能, 每个模块都有1个built-in 属性 _name_ , 如果这个模块被直接执行, 则个属性return的是"_main_" 字符串, 如果这个模块被别的模块调用执行, 则return 该模块的模块名(文件名去掉.py 后缀, 并且带包名)

这样我们就可以利用 判断 _name_ 是否等于 “_main_” 来决定当前文件是被直接执行还是被调用执行

测试:
我们修改m1.py代码, 把 _name_ print出来, 同时利用_name_ 与 “_main_” 的判断来 决定 f1 是否执行
m1.py

import os

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f1(from_file=filename):
    print("__name__ of m1 is {}".format(__name__))
    print("f1 in {} called from {}".format(filename, from_file))

def f2(from_file=filename):
    print("f2 in {} called from {}".format(filename, from_file))

if __name__ == "__main__":
    f1()
    f2()

这样, 当m1 被直接执行时, f1 还是会直接执行。 当run1.py import m1时, f1 不会被执行

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/m1.py
it's in m1.py
__name__ of m1 is __main__
f1 in m1.py called from m1.py
f2 in m1.py called from m1.py
(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
defined objects list: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'filename', 'm1', 'os']
__name__ of m1 is m1
f1 in m1.py called from run1.py

注意上面__name__的值




import 非同包内的对象

上面的例子 m1.py 和 run1.py 这个两个被引用和引用对象都是在同1个包内的。
下面就是不同包的例子
我们在 src/import_base/ 内再创建1个pkg文件夹, 里面添加模块m2.py

(.venv) [gateman@manjaro-x13 python_common_import]$ tree
.
├── pytest.ini
├── README.md
├── requirement.txt
├── src
│   ├── import_base
│   │   ├── __init__.py
│   │   ├── m1.py
│   │   ├── pkg
│   │   │   ├── __init__.py
│   │   │   └── m2.py
│   │   └── run1.py
│   └── __init__.py
└── test
    ├── __pycache__
    │   └── test_1.cpython-311-pytest-8.2.0.pyc
    └── test_1.py

简单来将就是m1.py 和 run1.py是同1层/同一包的, 但是pkg/m2.py 在下一层

m2.py

import os

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f3(from_file=filename):
    print("__name__ of m2 is {}".format(__name__))
    print("f3 in {} called from {}".format(filename, from_file))


if __name__ == "__main__":
    f3()

这时run1.py 直接引用 pkg.m2 是可以的

(.venv) [gateman@manjaro-x13 python_common_import]$ cat src/import_base/run1.py 
import os
import pkg.m2 as m2 # 注意这里要用as 假如模块名之前带了包命

filename = os.path.basename(__file__)
m2.f3(filename) # m2.f3 called from run1.py
(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m2.py
defined objects list: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'filename', 'm2', 'os']
__name__ of m2 is pkg.m2
f3 in m2.py called from run1.py
这次我们尝试在m2 里调用 m1 的f1

m2.py

import os
import m1

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f3(from_file=filename):
    print("__name__ of m2 is {}".format(__name__))
    print("f3 in {} called from {}".format(filename, from_file))

m1.f1(filename) # m1.f1 called from m2.py
if __name__ == "__main__":
    f3()

这时, 在run1.py 里调用m2 是没问题的

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
it's in m1.py
it's in m2.py
__name__ of m1 is m1
f1 in m1.py called from m2.py
defined objects list: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'filename', 'm2', 'os']
__name__ of m2 is pkg.m2
f3 in m2.py called from run1.py

注意这里的m2 的__name__是 pkg.m2 带包名的

但是直接执行m2.py 就会import m1失败

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/pkg/m2.py 
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/import_base/pkg/m2.py", line 2, in <module>
    import m1
ModuleNotFoundError: No module named 'm1'




import 与 sys.path的关系

要解释 上面的问题
为何 run1.py 调用m2时, m2 能正确引用m1, 但是直接执行m2.py 就会引用失败

我们要明白, python 要import 1个模块是必须在1组目录里查找 这些目录是否有该模块
这个目录组 就是 sys.path

为了查看上面的区别, 我们修改文件, 让其输出sys.path 的值
run1.py

import os

import sys
print("sys.path in run1.py is {}".format(sys.path))

import pkg.m2 as m2

filename = os.path.basename(__file__)

m2.f3(filename) # m2.f3 called from run1.py

m2.py

import os
import sys

print("sys.path in m2.py is {}".format(sys.path))

import m1

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f3(from_file=filename):
    print("__name__ of m2 is {}".format(__name__))
    print("f3 in {} called from {}".format(filename, from_file))

m1.f1(filename) # m1.f1 called from m2.py
if __name__ == "__main__":
    f3()

执行run1.py 时
输出:

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
sys.path in run1.py is ['/home/gateman/Projects/python/python_common_import/src/import_base', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages']
sys.path in m2.py is ['/home/gateman/Projects/python/python_common_import/src/import_base', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages']
it's in m1.py
it's in m2.py
__name__ of m1 is m1
f1 in m1.py called from m2.py
__name__ of m2 is pkg.m2
f3 in m2.py called from run1.py

可以简单, 无论在 run1.py 和 m2.py
sys.path 中, 除了一些built-in的path, 还会加入被执行文件(run1.py)的当前path
所以在 xxx/import_base是能找到 pkg.m2 和 m1
也就是将无论在run1.py 中引入 pkg.m2, 还是 引入 m2中 m1 都可以的, 因为都可以在
/home/gateman/Projects/python/python_common_import/src/import_base
里找到pkg.m2 和 m1

执行m2.py时
输出:

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/pkg/m2.py 
sys.path in m2.py is ['/home/gateman/Projects/python/python_common_import/src/import_base/pkg', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages']
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/import_base/pkg/m2.py", line 6, in <module>
    import m1
ModuleNotFoundError: No module named 'm1'

这时,sys.path 第一项就是 xxx/import_base/pkg 了, 里面只有m2 没有m1 所以引用失败

解决方法

解决方法很简单,
在m2 import m1 之前把m1 所在的path 加入 sys.path
下面是修改后的
m2.py

import os
import sys

sys.path.append("/home/gateman/Projects/python/python_common_import/src/import_base/")
print("sys.path in m2.py is {}".format(sys.path))

import m1

filename = os.path.basename(__file__)

print("it's in {}".format(filename))


def f3(from_file=filename):
    print("__name__ of m2 is {}".format(__name__))
    print("f3 in {} called from {}".format(filename, from_file))

m1.f1(filename) # m1.f1 called from m2.py
if __name__ == "__main__":
    f3()

这样无论执行run1.py or m2.py都没问提了

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/run1.py 
sys.path in run1.py is ['/home/gateman/Projects/python/python_common_import/src/import_base', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages']
sys.path in m2.py is ['/home/gateman/Projects/python/python_common_import/src/import_base', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages', '/home/gateman/Projects/python/python_common_import/src/import_base/']
it's in m1.py
it's in m2.py
__name__ of m1 is m1
f1 in m1.py called from m2.py
__name__ of m2 is pkg.m2
f3 in m2.py called from run1.py
(.venv) [gateman@manjaro-x13 python_common_import]$ python src/import_base/pkg/m2.py 
sys.path in m2.py is ['/home/gateman/Projects/python/python_common_import/src/import_base/pkg', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages', '/home/gateman/Projects/python/python_common_import/src/import_base/']
it's in m1.py
it's in m2.py
__name__ of m1 is m1
f1 in m1.py called from m2.py
__name__ of m2 is __main__
f3 in m2.py called from m2.py




解决 No module named ‘src’ 的错误

实际在项目编写代码中, 很多人仍然会把所有代码文件放入项目的src 文件夹中, test case 放入 test 文件夹中g

如:

(.venv) [gateman@manjaro-x13 python_common_import]$ ls -l
total 20
-rw-r--r-- 1 gateman gateman   21 May  4 01:59 pytest.ini
-rw-r--r-- 1 gateman gateman   22 May  4 01:06 README.md
-rw-r--r-- 1 gateman gateman   75 May  4 02:02 requirement.txt
drwxr-xr-x 3 gateman gateman 4096 May  4 23:51 src
drwxr-xr-x 3 gateman gateman 4096 May  4 19:10 test

而无论在哪个文件, 都是从src包开始引入, 就如java 一样
我们修改下文件
令 src/main.py -> src/import_base/run1.py -> src/import_base/pkg/m2.py

(.venv) [gateman@manjaro-x13 python_common_import]$ tree
.
├── pytest.ini
├── README.md
├── requirement.txt
├── src
│   ├── import_base
│   │   ├── __init__.py
│   │   ├── m1.py
│   │   ├── pkg
│   │   │   ├── __init__.py
│   │   │   └── m2.py ###
│   │   └── run1.py ###
│   ├── __init__.py
│   └── main.py ###
└── tes

main.py

import src.import_base.run1 as run1
run1.m2_f3()

run1.py

import os
import sys
print("sys.path in run1.py is {}".format(sys.path))

import src.import_base.pkg.m2 as m2

def m2_f3():
    filename = os.path.basename(__file__)
    m2.f3(filename) # m2.f3 called from run1.py

m2.py

import os
import sys

print("sys.path in m2.py is {}".format(sys.path))

filename = os.path.basename(__file__)

def f3(from_file=filename):
    print("__name__ of m2 is {}".format(__name__))
    print("f3 in {} called from {}".format(filename, from_file))

if __name__ == "__main__":
    f3()

这时直接调用main.py是会出错的

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/main.py 
Traceback (most recent call last):
  File "/home/gateman/Projects/python/python_common_import/src/main.py", line 1, in <module>
    import src.import_base.run1 as run1
ModuleNotFoundError: No module named 'src'

解决方法1:
在main import run1 之前 把src 包所在的path 加入sys.path

import sys
sys.path.append("/home/gateman/Projects/python/python_common_import")

import src.import_base.run1 as run1
run1.m2_f3()

但是这里hard code了项目位置
所以可以再修改为

import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

import src.import_base.run1 as run1
run1.m2_f3()

其实就是根据main.py的位置 获取上一层的位置, 就是项目位置
这样避免了hardcode, 项目部署在哪里都可以正确执行




更优雅地解决 No module named ‘src’ 的错误

上面的写法还是有碍观感

我在python 项目中是这样做的

额外在src/configs 里创建1个config.py
base_config.py

import yaml
import os
import sys
from loguru import logger

# append project path to sys.path
script_path = os.path.abspath(__file__)
project_path = os.path.dirname(os.path.dirname(os.path.dirname(script_path)))

print("project_path is {}".format(project_path))

# append project path to sys.path
sys.path.append(project_path)


# setup logs path
logger.add(os.path.join(project_path, "logs", "app.log"))

logger.info("basic setup done")


yaml_configs = None
# load additon configs.yaml
with open(os.path.join(project_path, "src", "configs", "config_dev.yaml")) as f:
    yaml_configs = yaml.load(f, Loader=yaml.FullLoader)

logger.info("all configs loaded")

然后在main.py 只需要引入 configs.config 就行了, 但是这里的configs之前不能带包src, 因为main.py在src内

import configs.config
import src.import_base.run1 as run1
run1.m2_f3()

这时 从main.py 主入口执行程序就不会有问题了, 后面所有的文件都可以 从 src包 开始 import

(.venv) [gateman@manjaro-x13 python_common_import]$ python src/main.py 
project_path is /home/gateman/Projects/python/python_common_import
2024-05-05 03:15:52.448 | INFO     | configs.config:<module>:19 - basic setup done
2024-05-05 03:15:52.449 | INFO     | configs.config:<module>:27 - all configs loaded
sys.path in run1.py is ['/home/gateman/Projects/python/python_common_import/src', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages', '/home/gateman/Projects/python/python_common_import']
sys.path in m2.py is ['/home/gateman/Projects/python/python_common_import/src', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/gateman/Projects/python/python_common_import/.venv/lib/python3.11/site-packages', '/home/gateman/Projects/python/python_common_import']
__name__ of m2 is src.import_base.pkg.m2
f3 in m2.py called from run1.py




如何解决 临时 debug 某个module 文件的问题

例如现在的run1.py 是这样的

import os
import sys
print("sys.path in run1.py is {}".format(sys.path))

import src.import_base.pkg.m2 as m2

def m2_f3():
    filename = os.path.basename(__file__)
    m2.f3(filename) # m2.f3 called from run1.py

单独执行这个run1.py 还是会出错, 找不到src的包嘛

命令执行

方法1:
如何在命令执行, 我建议直接在文件开头加上
import sys
sys.path.append(《project path》)

临时执行的话, hardcode 改一下

方法2:
执行前 把project 目录 set 如PYTHONPATH 没错, sys.path 也会把PYTHONPATH的值加上!

import os
import sys
print("sys.path in run1.py is {}".format(sys.path))

import src.import_base.pkg.m2 as m2

def m2_f3():
    filename = os.path.basename(__file__)
    m2.f3(filename) # m2.f3 called from run1.py
pycharm

其实大部分debug 都是在IDEA 上执行的
而pycharm 已经默认在执行每个文件都把项目的path 加入系统变量 PYTHONPATH了

vscode

重点来了
vscode 的debug 设置是 launch.json
加上PYTHONPATH的配置

"configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {"PYTHONPATH": "${workspaceFolder}"}
        }
    ]

在这里插入图片描述

后记

写的真是长啊
希望本文可以解决你的大部分关于python的问题!

  • 26
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值