Python consda

368 阅读3分钟

我用的是pycharm编辑器

一、为什么用consda?

方便切换python 版本

以前:

  • 下载的包不好管理
  • 只用一个总的环境,不兼容包的多版本
  • 单个项目自己的虚拟环境(项目下的venv)没必要,不复用

现在:

image.png



二、怎么安装consda?

(转)2024年最新版Anaconda3的安装配置及使用教程(非常详细) blog.csdn.net/liuhyusb/ar…


三、 conda常用命令

conda env list // 查看有多少个虚拟环境  或者 conda info --envs
conda activate py312 // 切换到指定的env
conda info //当前项目的虚拟环境的信息
conda search [package]  //搜索某个包  conda search scrapy

#镜像源管理
#清除缓存

四、碰到的问题:

4.1. pip无法安装,总是超时错误

pip install xxxx 报错

pip WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) 
after connection broken by 
'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end 
closed connection without response'))': /simple/aiofiles/

配置国内源,没想到pip.ini配置错误了:几个空格tab键的问题

原来的配置文件位置: C:\Users\ZP\AppData\Roaming\pip\pip.ini

#错误配置
[global]
timeout=4000
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url=
        https://pypi.org/simple/pip/
        https://pypi.tuna.tsinghua.edu.cn/simple
        http://pypi.douban.com/simple/
        http://pypi.mirrors.ustc.edu.cn/simple/
        http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=
        pypi.tuna.tsinghua.edu.cn
        pypi.douban.com
        pypi.mirrors.ustc.edu.cn
        mirrors.aliyun.com

修改后,好了:

[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

用到的查看报错日志命令:pip install aiofiles -vv


4.2. pip安装lib包,遇到包的版本问题

安装命令 pip install -r ./requirements.txt

遇到报错subprocess-exited-with-error,日志如下:

error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [33 lines of output]
    Traceback (most recent call last):
      File "C:\Users\ZP\AppData\Roaming\Python\Python312\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
        main()
      File "C:\Users\ZP\AppData\Roaming\Python\Python312\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\ZP\AppData\Roaming\Python\Python312\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 112, in get_requires_for_build_wheel
        backend = _build_backend()
                  ^^^^^^^^^^^^^^^^
      File "C:\Users\ZP\AppData\Roaming\Python\Python312\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 77, in _build_backend
        obj = import_module(mod_path)
              ^^^^^^^^^^^^^^^^^^^^^^^
      File "D:\Program11\anaconda3\Lib\importlib\__init__.py", line 90, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 995, in exec_module
      File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
      File "C:\Users\ZP\AppData\Local\Temp\pip-build-env-n0p2lcey\overlay\Lib\site-packages\setuptools\__init__.py", line 16, in <module>
        import setuptools.version
      File "C:\Users\ZP\AppData\Local\Temp\pip-build-env-n0p2lcey\overlay\Lib\site-packages\setuptools\version.py", line 1, in <module>
        import pkg_resources
      File "C:\Users\ZP\AppData\Local\Temp\pip-build-env-n0p2lcey\overlay\Lib\site-packages\pkg_resources\__init__.py", line 2172, in <module>
        register_finder(pkgutil.ImpImporter, find_on_path)
                        ^^^^^^^^^^^^^^^^^^^
    AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
    [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

pkgutil内部方法,不好修改,

发现是安装numpy==1.25.2引起的, github上numpy现在的版本是numpy==2.0.1

提高numpy的版本,单独安装, 然后删掉requirements.txt中的numpy


4.3. PyCharm编辑器的权限问题

Downloading and Extracting Packages: ...working... done
Preparing transaction: ...working... done
Verifying transaction: ...working... failed

EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
  environment location: D:\Program11\anaconda3\envs\py312

提高PyCharm运行权限,管理员运行

一次性:右击图标管理员运行

永久性:PyCharm 属性,兼容性,管理员运行


4.4. 明明安装了lib包,但是还是提示没有安装

venv环境不一致

比如:from fake_useragent import UserAgent

pip 已经安装好了fake_useragent,但是项目中还是标红,提示fake_useragent没有安装

解决方法

方法1,命令行切换到自己的虚拟环境,再通过pip安装

conda activate py312
#切换后再运行安装命令
pip install fake_useragent

方法2. pycharm 中查看自己项目的依赖 interpreter解释程序

image.png

找找\xxxx\Lib\site-packages\包的目录下fake_useragent包,复制粘贴到自己的环境下的site-packages中