简体   繁体   English

Linux中的Python和python3有不同的pip吗?

[英]Is Python and python3 in Linux has different pip?

I installed Django on my Ubuntu 21.04 Software but when I use:我在我的 Ubuntu 21.04 软件上安装了 Django ,但是当我使用时:

python manage.py runserver 

I get an error massage: No module named “Django” But when I use:我收到错误消息:没有名为“Django”的模块但是当我使用时:

python3 manage.py runserve

It works fine but my python —-version is 3.10.1 and python3 —version is 3.9.x So what is the error and how I can run it with python only not python3它工作正常,但我的 python —版本是 3.10.1 和 python3 —版本是 3.9.x 那么错误是什么以及如何使用 python 运行它而不是 python3

The issue here is that the python and python3 commands are pointing to two different Python installations/environments altogether.这里的问题是pythonpython3命令指向两个不同的 Python 安装/环境。

It looks like you installed Django in your Python 3.9.x environment (the one which you access by typing python3 ).看起来您在 Python 3.9.x 环境(您可以通过键入python3访问的环境)中安装了 Django 。

On the other hand, your Python 3.10.1 environment (which you access by writing python ) seems to have no Django installed.另一方面,您的 Python 3.10.1 环境(您可以通过编写python访问)似乎没有安装 Django。

The pip / pip3 commands are sometimes confusing, and may point to a different Python installation than the one you think they do. pip / pip3命令有时会令人困惑,并且可能指向与您认为的不同的 Python 安装。 This depends entirely on how you set up your Python environments in your machine.这完全取决于您如何在机器中设置 Python 环境。

In order to access the pip of a specific Python environment, the best way is to run it as a module, eg:为了访问特定pip环境的 pip ,最好的方法是将其作为模块运行,例如:

python -m pip [...]

This guarantees that the pip you're using is the one associated with the Python environment you're evoking with the python command.这保证了您使用的pip与您使用python命令调用的 Python 环境相关联。

So in order to install Django on your Python 3.10.1 environment, you need to run:因此,为了在您的 Python 3.10.1 环境中安装 Django,您需要运行:

python -m pip install django

If you use PIP3 to install the module, it will only be installed for Python3.如果您使用 PIP3 安装该模块,它将只为 Python3 安装。 If you use PIP to install the module, the system will use the Python version that is listed first in the PATH variable.如果您使用 PIP 安装模块,系统将使用 PATH 变量中首先列出的 Python 版本。

You have multiple Python instances on your machine.您的机器上有多个 Python 实例。 To avoid such a problems in the future, it is generally advised to use Virtualenv in Python, if you run code on your general-purpose device.为了避免以后出现此类问题,如果您在通用设备上运行代码,通常建议使用 Python 中的 Virtualenv。 (and have multiple Python instances on your machine) (并且您的机器上有多个 Python 实例)

In order to configure Virtualenv that run the following commands:为了配置运行以下命令的 Virtualenv:

sudo apt install python3-venv python3-pip python3 -m venv venv sudo apt install python3-venv python3-pip python3 -m venv venv

Then you activate virtual environment: source venv/bin/activate然后激活虚拟环境: source venv/bin/activate

Once the virtual environment is activated, you can install Python modules in this specific virtual environment, this way you will not have to mess up with issues such as the one you have described:激活虚拟环境后,您可以在此特定虚拟环境中安装 Python 模块,这样您就不必搞砸诸如您描述的问题:

pip install django

and then if you run:然后如果你运行:

python manage.py runserver , then it would be running in the context of the specific virtual environment. python manage.py runserver ,那么它将在特定虚拟环境的上下文中运行。

I know it is not a direct answer to your problem, but I believe it will help you in avoiding such an issues in the future.我知道这不是您问题的直接答案,但我相信它将帮助您避免将来出现此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM