简体   繁体   中英

How to properly install Python and Django

I'm trying to set up a Python/Django dev environment on my LinuxMint 19.1 machine. I installed the newest version of Python 3.7.3, Pip and Django. It seemed to be working until I figured out it was not using the correct version of Python. All attempts to correct this have been difficult.

When I typed python -V it would display Python 2.x. When I typed python3 -V it would display Python 3.6.8. So I updated the alternatives and configured python so now when I type python -V I get Python 3.7.3 and when I type python3 -VI still get Python 3.6.8. I saw that I had to reinstall Django for it to be associated with the new version of Python so I tried to install again using Pip but got an error (the same ModuleNotFoundError: No module named 'pip' error as below). So I uninstalled Pip and reinstall it. I went ahead and installed Pip.

sudo apt install python-pip

This reported that it worked fine. I then tried to install Django and got the same error again.

pip install django
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'

So now I'm not sure how to proceed. I'm not sure if I did something wrong to create this issue.

Any help would be appreciated.

python-pip3 is for python3

After you've installed package python-pip3 you'll need to run pip3 install xxxx correspondingly.

All of the above is not related to the alternatives though.

Python 2.x comes with Linux system. just use pip3 install django

最好使用pipenv install djangopoetry add django因为它是虚拟环境附带的。

As mentioned, your problem is because on Linux, pip is used for python2 and pip3 is for python3. Refer to this article for step by step instructions on installing pip3 or pip on Linux.

It also sounds like you are struggling to manage your python versions, so I would recommend using pyenv .

For mac, this is how I set up my laptop (note minor version subject to change).

execute in terminal

brew install pyenv
pyenv install 3.7.1
pyenv install 3.6.6
pyenv install 2.7.15
pyenv global 3.7.1 3.6.6 2.7.15

Add to bash profile:

#pyenv
PATH="$(pyenv root)/shims:${PATH}"
export PATH
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

For further details on managing the pip packages that you install, see pipenv . Since you will want to know what version of django and other dependencies are installed and be able to easily update once you have your project underway. If you go this route, you will eliminate these issues since the install would then be pipenv isntall django instead of pip3 install django .

First check whether you can access to Python 3.7 shell by running python3.7 command. You will see something like this.

Python 3.7 外壳

If it's working, don't worry. Few more steps to go. Then install or upgrade your pip on Python 3.7 . Run python3.7 -m pip install --upgrade pip command.

为 Python 3.7 安装或升级 pip

You can install any pip package in this way. python3.7 -m pip install Django .

为 Python 3.7 安装 Django

I got the permission error, so I ran it with sudo privileges.

python3.7 manage.py makemigrations
python3.7 manage.py migrate
python3.7 manage.py runserver

python3.7 manage.py <your_command>

In this way you can run your Django project which you created for Python 3.7 .

According to you, you have installed Python 3 (3.7.3), and pip comes already installed since Python 3 > 3.4. In addition, you are saying that you have set your machine so when you run python -V , you get 3.7.3. You can install django by running the following command:

$ python -m pip install django

However, the recommended way to install a package is to create a virtual environment . Then, you can follow up the next steps:

$ python -m venv venv # this will create a virtual environment called venv
$ source venv/bin/activate # activates your virtual environment 
$ pip install django # installs django in your virtual environment

This will install django in a virtual environment. Notice that you will have an indicator that you are using a virtual environment in your terminal. (venv) $

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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