问题
I just upgraded to python 3.7
and I realized that all my modules stuck with the previous version. Even Django is not recognised anymore. How can I do to transfer everything to the new version? I am a little lost right now, don't even know where the new version has been installed.
Edit:
When I do $ which python3.6
the terminal tells me it doesn't exist, but I have a python3.6
directory in /usr/local/lib/
, where all modules are installed.
In the same directory /usr/local/lib/
I also have a python3.7
directory with some modules installed but many are missing. However when I search for the file python3.7
in my finder it doesn't appear. when I do $ which python3.7
the path is /usr/local/bin
so not the same path as the directory.
Anyone sees what happened and knows how I can transfer all modules to python3.7
?
回答1:
Even if the old python version has been removed, it is possible to use the pip of the current python version with the --path
option to list all the modules installed in the previous version.
For example, migrating all my user installed python modules from 3.7 to 3.8
pip freeze --path ~/.local/lib/python3.7/site-packages > requirements.txt
pip install --user -r requirements.txt
Incidentally, I always use pip install
with --user
and leave the system wide installations to the package manager of my linux distro.
回答2:
It is safer to re-install all packages due to possible compatibility issues:
pip3.6 list | awk '{print $1}' | xargs -I{} pip3.7 install {}
回答3:
In some cases, we don't have the opportunity to pip freeze
in old version--because I've already updated and old version have been purged! There are some measures I've taken to recover some of the packages but I'm NOT sure every package would work with this fix.(e.g. the packages built with wheels)
mv /your/path/to/python3.{6,7}/site-packages/
- If the case is packages installed outside venv (in
/usr/local/
or~/.local
), reinstall pip with get-pip.py, just to be safe. - If you are recovering a virtualenv. Activate your virtualenv and use my script
Most of your packages should work by now. If anything malfunctions, pip reinstall
would works. If you still want it 100% works, pip freeze
now.😉
回答4:
I'm not sure about all modules...but if you want to install a module specifically in python3.7, try this:
python3.7 -m pip install *module_name*
回答5:
I have an alternative
(Not sure if works outside Windows 10)
I'm currently migrating from 3.7 to 3.8 and the way I found to re-install my previous libraries was by using a script I had that updates all packages via pip install. (Assuming you installed your new Python version as your main version) This checks for all the packages I had and updates/install them in the new Python version.
Note: I prefer to run the script from the command line
- Use the file explorer to go to the folder where you have the script;
- Click on the path box, write "cmd" and press
enter
to open a command line from the folder where you are; - Write "python name_of_your_script.py" and press
enter
to run the command.
The script (adapted from this solution):
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
[call("pip install " + name + " --upgrade") for name in packages]
回答6:
I faced a similar problem, now that I upgraded from python 3.7 to python 3.8 (new)
I installed Python 3.8, but the system kept the python37 subfolder with the already installed packages(...\Python37-32\Lib\site-packages) even with the Pyhton38 subfolder created, with the new python.exe.
Usually, it's possible to keep on using the old libraries on your new Python version, because the existent libraries installation folder are already registered in your local computer system path (*).
Even though, I've had problems to use some libraries (some worked in Jupyter Notebook but not in Spyder). I tried the alternatives others proposed to migrate libraries but it did not worked (maybe I did not
So I used brutal force solution.. Not elegant at all, but it worked:
remove the OLD python version folders from the system path or even remove the folder itself for good.. Folders: C:\Users\USERNAME\AppData\Roaming\Python\Python37 C:\Users\USERNAME\AppData\Local\Programs\Python\Python37
Reinstall the packages you need, preferably via Anaconda prompt. python -mpip install library_name OR pip install --user --force-reinstall package_name OR pip install --user --force-reinstall package_name == specify_package_version
The libraries will be installed at c:\users\USERNAME\anaconda3\lib\site-packages and recognized by your new python version.
(*) to add the folder to the PATH: System properties --> environment variables --> click "Path"--> edit --> add folder name)
来源:https://stackoverflow.com/questions/51308683/how-to-move-all-modules-to-new-version-of-python-from-3-6-to-3-7