简体   繁体   English

我如何'强迫'python使用特定版本的模块?

[英]How do I 'force' python to use a specific version of a module?

I'm new to python so I apologize if this has been answered elsewhere with tags I haven't thought of. 我是python的新手,所以我很抱歉,如果已经在其他地方用我没有想过的标签回答了这个问题。

I'm trying to update numpy from the 1.6 version I have now to 1.8. 我正在尝试将numpy从我现在的1.6版本更新为1.8。 I've installed numpy in my python site-packages when I call numpy it calls the old 1.6 version. 我在我的python site-packages中安装了numpy,当我调用numpy时,它调用旧的1.6版本。 I've tried looking for the root to numpy 1.6 so I can remove it but that leads to :- 我已经尝试寻找根到numpy 1.6所以我可以删除它,但这导致: -

import numpy
print numpy.__version__
print numpy.__file__
>>>
1.6.2
V:\Brian.140\Python.2.7.3\lib\site-packages\numpy\__init__.pyc

I've added the folder containing the module to the system path using:- 我使用以下方法将包含模块的文件夹添加到系统路径: -

sys.path.append('C:/Python27/Lib/site-packages')

and I know this works as I can call other modules in this location with no errors, for example:- 我知道这可行,因为我可以在此位置调​​用其他模块而没有错误,例如: -

import wx
import Bio

and

import nose

produce no errors. 不产生任何错误。 Why is this happening and how can I tell python which version of numpy to use? 为什么会发生这种情况?如何告诉python使用哪个版本的numpy?

You can also insert the directory to the beginning of the path, so you won't need to remove the old one: 您还可以将目录插入路径的开头,因此您不需要删除旧目录:

sys.path.insert(1, 'C:/Python27/Lib/site-packages')

That won't work if you've already import your module. 如果您已导入模块,那将无效。 You can either import it after the sys.path.insert command, or use importlib.reload( module_name ) 您可以在sys.path.insert命令之后导入它,也可以使用importlib.reload( module_name

This is a very messy solution and probably shouldn't be encouraged but I found that if I remove the location of the old version of numpy from the system path I can call the version I want. 这是一个非常混乱的解决方案,可能不应该鼓励,但我发现,如果我从系统路径中删除旧版本numpy的位置,我可以调用我想要的版本。 The specific lines were:- 具体路线是: -

import sys
sys.path.append('C:/Python27/Lib/site-packages')
sys.path.remove('V:\\\Brian.140\\\Python.2.7.3\\\Lib\\\site-packages')
import numpy

Force python to use an older version of module (than what I have installed now) mentions a generic solution: 强制python使用旧版本的模块(比我现在安装的版本)提到了一个通用的解决方案:

 import pkg_resources pkg_resources.require("numpy==`1.16.2") # modified to use specific numpy import numpy 

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

相关问题 强制屏幕会话使用特定版本的python - Force screen session to use specific version of python 我安装了 2 个版本的 python,但 cmake 使用的是旧版本。 如何强制 cmake 使用较新版本? - I have 2 versions of python installed, but cmake is using older version. How do I force cmake to use the newer version? 我如何使用类型来强制使用特定的枚举作为 python 中的参数 - How do I use typing to force using a specific enum as an argument in python 强制 python 使用旧版本的模块(比我现在安装的版本) - Force python to use an older version of module (than what I have installed now) 如何在不同版本的python上安装python模块 - How do I install a python module on a different version of python 如何为特定的python版本安装python模块 - how to install python module for specific python version 如何使命令行使用特定版本的python? - How do I make my command line use a specific version of python? 如何在python脚本中使用蛋黄来获取特定软件包的最新版本? - How do I use yolk from within a python script to get the latest version for a specific package? 如何强制iPython使用旧版本的Python? - How to force iPython to use an older version of Python? 如何强制iPython使用旧版本的Python? - How to force iPython to use an older version of Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM