为 pipenv 项目中的 python 文件创建命令行别名 [英] making a commandline alias to a python file in a pipenv project

查看:41
本文介绍了为 pipenv 项目中的 python 文件创建命令行别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 pipenv 制作一个 python 项目,我希望能够从我的 (linux) 系统上的任何位置在终端中运行它.具体来说,假设我有以下目录结构:

/home/项目文件主文件/other_dir

我希望能够创建一个别名,允许我像这样调用 main.py:

/home/other_dir$ alias_to_my_proyect --some args

并在虚拟环境中运行它,具有与

相同的行为

/home/project$ pipenv run python main.py

但在另一个目录中.

如果不是pipenv项目,我就在文件的开头使用shebang,然后在.bashrc中给它添加一个别名,但是我想使用pipenv的虚拟环境,但是我找不到一种使用 pipenv 做到这一点的方法.

解决方案

如果您想为您的脚本使用特定的 Python 环境,您需要将其指向该环境的解释器.在 Mac 上,默认情况下 pipenv 将所有 virtualenvs 安装到 /Users//.local/share/virtualenvs/ 但是可以将其设置为不同的位置 手册中的描述:

<块引用>

Pipenv 会自动遵守 WORKON_HOME 环境变量,如果你设置了它——这样你就可以告诉 pipenv 在你想要的任何地方存储你的虚拟环境,例如:

导出 WORKON_HOME=~/.venvs

此外,您还可以通过设置 PIPENV_VENV_IN_PROJECT 环境变量,让 Pipenv 将 virtualenv 粘贴到 project/.venv 中.

您可以使用项目文件夹中的 pipenv --venv 找出 virtualenv 的确切位置.它返回类似于 /Users/reedef/.local/share/virtualenvs/project-BpR9WgCa 的内容.解释器位于该位置的 ./bin/python 中.

如果我们假设您没有设置任何环境变量并且您使用的是 Mac,那么这意味着您可以编写脚本:

#!/usr/bin/env sh/Users/reedef/.local/share/virtualenvs/project-BpR9WgCa/bin/python/home/project/main.py

并将其放在 $PATH 中的某个位置,例如/usr/local/bin/my_fancy_main 让它在特定环境中运行.

注意:正如@Jon 在评论中提到的,路径末尾的 -BpR9WgCa 是稳定的,因为它 由项目路径制作:

<块引用>

hash = hashlib.sha256(location.encode()).digest()[:6]

只要项目路径没有改变就应该是一样的.

I've been making a python project using pipenv, and I want to be able to run it in a terminal from any location on my (linux) system. Specifically, say I have the following directory structure:

/home
  /project
    Pipfile
    main.py
  /other_dir

I would like to be able to make an alias that allows me to call main.py like so:

 /home/other_dir$ alias_to_my_proyect --some args

and run it in the virtual env, having the same behaviour as

/home/project$ pipenv run python main.py

But in another directory.

If it weren't a pipenv project, I'd just use a shebang a the start of the file and then add an alias to it in my .bashrc, but I want to use pipenv's virtual environment, but I cant find a way to do this with pipenv.

解决方案

If you want to use a specific python environment for your script you will need to point it to the interpreter of that environment. On Mac the default is that pipenv installs all virtualenvs to /Users/<user_name>/.local/share/virtualenvs/ however that can be set to different locations as described in the manual:

Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:

export WORKON_HOME=~/.venvs

In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.

You can find out where the exact location of the virtualenv is with pipenv --venv inside your project folder. It returns something like /Users/reedef/.local/share/virtualenvs/project-BpR9WgCa. The interpreter is in ./bin/python of that location.

If we assume that you did not set any environment variable and you are using Mac than that means that you can write a script:

#!/usr/bin/env sh
/Users/reedef/.local/share/virtualenvs/project-BpR9WgCa/bin/python /home/project/main.py

and place it somewhere in your $PATH, e.g. /usr/local/bin/my_fancy_main to let it run in that specific environment.

Note: as mentioned by @Jon in the comments, -BpR9WgCa at the end of the path is stable as it is made from the project path:

hash = hashlib.sha256(location.encode()).digest()[:6]

It should be the same as long as the project path hasn't changed.

这篇关于为 pipenv 项目中的 python 文件创建命令行别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆