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