在所需的文件夹中安装python软件包 [英] Installing a python package in a desired folder

查看:105
本文介绍了在所需的文件夹中安装python软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ubuntu机器上下载了一个python软件包进行安装.该软件包已经有一个setup.py文件可以使用,但是我想将默认的python安装地址更改为其他名称,特别是针对此软件包(并非永久有效).所以我尝试的是:

I have downloaded a python package to install, on my ubuntu machine. The package has already a setup.py file to use, but I want to change the default python installation address to something else, for this package specifically (and not for good). So what I tried is:

首先在终端中,导出新文件夹的地址: export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages

First in the terminal, I export that address of the new folder: export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages

然后我将此导出的地址添加为安装命令的前缀: python setup.py install --prefix=~/Documents/testfolder

Then I add this exported address as prefix to the installation command: python setup.py install --prefix=~/Documents/testfolder

安装完成.现在,为了使python总是也寻找这个新路径(默认安装路径旁边),我将地址导出到bashrc文件中: export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"

The installation goes through. Now to make python always look for this new path as well (next to the default installation path), I export the address in bashrc file: export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"

但是现在每当我打开终端并尝试导入已安装的程序包时,它都看不到(没有模块命名为...").只有在我有安装文件的文件夹(即setup.py)中打开终端并运行python时,它才能看到该软件包,并且可以在其中运行.

But now whenever I open a terminal and try to import the installed package, it cannot see ("no module named..."). Only when I open a terminal in the folder where I had the installation files (namely setup.py), and run python, can it then see the package, and it works there.

为什么我在bashrc中无法导出,从而使该软件包可从任何地方使用? 以上是我做错了什么吗?

Why isn't my export in bashrc making the package available from anywhere? Is there something I have done wrong in the above?

推荐答案

回答有关导出路径的问题.您是否将$ PYTHONPATH作为$ PATH的一部分?如果没有,则应将其添加到路径.

To answer your question about the export path. Do you have $PYTHONPATH as a part of your $PATH? If not you should add it to path.

我认为处理这种情况的最佳方法是使用虚拟python环境.有几个可供选择,但我最喜欢virtualenv.采用这种方法的原因是因为您可以在单独的文件夹中管理不同版本的python.并在这些文件夹中安装了单独的软件包.我建议您对其进行研究,因为它是一个非常有用的工具.如果您想了解如何使用它,我可以提供 https://virtualenv.pypa.io /en/stable/

The best way to handle this scenario in my opinion is to use a virtual python environment. There are a couple to choose from, but I like virtualenv the best. The reason to take this approach is because you can manage different versions of python in separate folders. And have separate packages installed in these folders. I recommend looking into it as it is a very useful tool. If you want an examole of how to use it i can provide that https://virtualenv.pypa.io/en/stable/

这篇关于在所需的文件夹中安装python软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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