Linux下修改conda环境下的pip安装目录

一、问题

        我在linux服务器上新建一个conda环境后,在pip安装包的时候,老是出现我已在其他目录下安装的提示(如下所示)。我的pip安装路径应该是/home/Bxl/.conda/envs/hjw01/lib/python3.6/site-packages/,而不是/home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/。

(hjw01) [Bxl@inspur whl]$ pip install requests-2.21.0-py2.py3-none-any.whl
Processing ./requests-2.21.0-py2.py3-none-any.whl
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/urllib3-1.24.3-py3.6.egg (from requests==2.21.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/chardet-3.0.4-py3.6.egg (from requests==2.21.0)
Requirement already satisfied: certifi>=2017.4.17 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages (from requests==2.21.0)
Collecting idna<2.9,>=2.5 (from requests==2.21.0)

二、解决方法

        我采取的解决思路就是看pip的安装目录是否已经设定,通过一番查找后,发现是安装路径出了问题。解决方法如下:

        在激活conda环境的前提下,然后修改 site.py 中的USER_BASE和USER_SITE变量,site.py路径:/home/Bxl/.conda/envs/hjw01/lib/python3.6/site.py,修改后内容如下:(之后重启conda环境)

        

ImportError exception, it is silently ignored.
"""

import sys
import os
import builtins
import _sitebuiltins

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
ENABLE_USER_SITE = None

# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = '/home/Bxl/.conda/envs/hjw01/lib/python3.6/site-packages'
USER_BASE = '/home/Bxl/.conda/envs/hjw01'


def makepath(*paths):
    dir = os.path.join(*paths)
    try:
        dir = os.path.abspath(dir)
    except OSError:
        pass
    return dir, os.path.normcase(dir)


def abs_paths():
    """Set all module __file__ and __cached__ attributes to an absolute path"""
    for m in set(sys.modules.values()):
        if (getattr(getattr(m, '__loader__', None), '__module__', None) not in
                ('_frozen_importlib', '_frozen_importlib_external')):
            continue   # don't mess with a PEP 302-supplied __file__
        try:
            m.__file__ = os.path.abspath(m.__file__)
        except (AttributeError, OSError):
            pass
        try:
            m.__cached__ = os.path.abspath(m.__cached__)
        except (AttributeError, OSError):
            pass

 

### 如何在 Conda 环境中正确使用 Pip 命令 在 Conda 环境下是可以正常使用 `pip` 命令的,但需要注意一些细节以确保两者不会冲突。当激活某个特定的 Conda 环境时,该环境中的 Python 和 `pip` 将优先于全局或其他环境下的版本被调用[^2]。 为了验证当前使用的 `pip` 是否属于已激活的 Conda 环境,可以通过以下命令确认: ```bash which pip # Linux 或 macOS 下使用 where pip # Windows 下使用 ``` 如果返回路径指向的是当前 Conda 环境目录,则说明正在使用正确的 `pip` 实例。 #### 使用 Pip 安装到指定 Conda 环境 要通过 `pip` 在特定 Conda 环境安装软件,需先激活目标环境再运行 `pip install` 命令。例如,在名为 `myenv` 的环境安装 NumPy 库,操作流程如下: 1. **激活 Conda 环境** ```bash conda activate myenv ``` 2. **使用 Pip 进行安装** 如果希望加速下载过程,可以借助清华大学开源镜像源: ```bash pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 此方式不仅限于单个,还可以扩展至其他依赖项的批量安装[^1]。 #### 配置永久性的 Pip 加速设置 对于频繁需要从国内镜像站点获取资源的情况,建议配置本地用户的 `.pip/pip.conf` 文件实现持久化更改。具体做法是在用户主目录创建或编辑 `~/.pip/pip.conf` (Windows 上可能是 `%APPDATA%\pip\pip.ini`),加入以下内容来默认启用清华 TUNA 源: ```ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple ``` 完成上述修改之后,无需每次都手动附加 `-i` 参数即可享受更快的速度[^3]。 #### 注意事项 尽管可以在 Conda 中混合使用 `conda install` 和 `pip install` 来管理库,但这可能会引起兼容性问题或者破坏虚拟环境的一致状态。因此推荐尽可能只选用一种工具为主力管理者;若有特殊需求必须同时运用二者的话,请务必小心处理潜在矛盾之处。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值