python ImportError: No module named ‘xxx‘

https://github.com/kennethreitz/setup.py

Python导入自定义模块ImportError: No module named 'xxx'问题_芥末鱿鱼~的博客-CSDN博客

从原理上说,上面这篇博客已经讲的很清楚了。但是需要注意一点,比如我此时已经到了这个目录下,运行

python3 ./tests/test_jupyterint.py

目录结构为:

.
├── LICENSE
├── README.md
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── kishu
│           ├── __init__.py
│           └── jupyterint.py
├── dist
│   └── kishu-0.1.0-py3.8.egg
├── install.sh
├── kishu
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   └── jupyterint.cpython-38.pyc
│   └── jupyterint.py
├── kishu.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
├── requirements.txt
├── setup.py
├── tests
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── context.cpython-38.pyc
│   │   ├── test_basic.cpython-38-pytest-7.0.1.pyc
│   │   └── test_jupyterint.cpython-38-pytest-7.0.1.pyc
│   ├── context.py
│   ├── test_basic.py
│   └── test_jupyterint.py
└── upload2pypi.sh

依然会报错。原因很简单,无论你在哪里运行python + 文件名,都会默认路径你当前路径为那个文件所在的目录。而要解决这个问题,有两个方法。

1. 你得把你想要的目录整个作为一个包install到你的虚拟环境中,install的代码如下:

python setup.py install

至于setup.py的模板,可以参考

https://github.com/kennethreitz/setup.py

2. 把整个工程的根目录添加到当前的虚拟环境中。

其实就是在site-package里新建一个.pth文件。

具体方法如下:

Python如何永久修改sys.path_俯仰天地的博客-CSDN博客

我的site-package目录在这里:

虚拟环境目录/lib/python3.8/site-packages

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值