问题
Issue
I would like to install with pip3
a python module from github into a local folder named local_lib/
and then use it in a script, without any virtualenv.
Context
Here is my folder structure :
.
+-- local_lib/ // Folder where the package must be installed
+-- my_script.py
Here is the command line i use to install the path.py
package from github into the local_lib/
folder :
pip3 install --upgrade --target local_lib git+https://github.com/jaraco/path.py.git
Here is the content of the local_lib/
folder after the command line :
.
+-- local_lib/ // Folder where the package must be installed
| +-- __pycache__
| +-- importlib_metadata-0.8.dist-info
| +-- path.py-11.5.1.dev20+g3684c4d.dist-info
| +-- zipp-0.3.3.dist-info
| +-- importlib_metadata
+-- my_script.py
Here is the content of my_script.py
:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from local_lib.path import Path
if __name__ == '__main__'::
print(Path('.') / 'generated_folder')
But when i execute the script with python3 my_script.py
, i get the following error of import :
Traceback (most recent call last):
File "my_program.py", line 4, in module
from local_lib.path import Path
ModuleNotFoundError: No module named 'local_lib.path'
Should i change the way i import the package into my_scipt.py or should i change the command line to install the package ?
回答1:
You have to tell Python that it has to look in local_lib
for modules. E.g. by adding it to sys.path in your script (before importing from it) or by adding it to your PYTHONPATH environment variable.
回答2:
For this, you can just download the path.py file into your local_lib
folder and your path.py use case should work. You don't need install it with pip3
.
Should you want path.py
to be available for scripts in any folder on the machine, using the same syntax, use the solution from Roland Smith or install it the generic way with pip install path.py
.
来源:https://stackoverflow.com/questions/54052307/install-a-python-package-module-from-github-in-local-folder-an-use-it