小编Jen*_*nny的帖子

Python:从多个已安装的模块版本中选择一个

在我的系统上,我有多次安装了几个模块.举个例子,numpy 1.6.1安装在标准路径中/usr/lib/python2.7/dist-packages,我有一个numpy 1.8.0安装的更新版本/local/python/lib/python2.7/site-packages/.

我不能简单地删除旧版本的原因是我没有权限在我的工作计算机上更改任何内容.但是我需要使用新的numpy版本.

我加入/local/python/lib/python2.7/site-packages/了我的PYTHONPATH.不幸的是,这没有用,因为/usr/lib/python2.7/dist-packages首先插入到路径中,因此numpy 1.6.1将被加载.这是一个例子:

>>> import os
>>> print os.environ['PYTHONPATH']
/local/python/lib/python2.7/site-packages
>>> import pprint
>>> import sys
>>> pprint.pprint(sys.path)
['',
 '/local/python/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg',
 '/local/python/lib/python2.7/site-packages/pyparsing-2.0.1-py2.7.egg',
 '~/.local/lib/python2.7/site-packages/setuptools-3.4.4-py2.7.egg',
 '~/.local/lib/python2.7/site-packages/mpldatacursor-0.5_dev-py2.7.egg',
 '/usr/lib/python2.7/dist-packages',
 '/local/python/lib/python2.7/site-packages',
 '/usr/lib/python2.7',
 ...,
 '~/.local/lib/python2.7/dist-packages', 
 ...]
Run Code Online (Sandbox Code Playgroud)

所以,似乎导入订单是

  1. 当前目录
  2. 鸡蛋来自 PYTHONPATH
  3. 来自本地模块路径的鸡蛋(~/.local/lib/python2.7/site-packages/*.egg)
  4. 系统范围的模块路径(~/usr/lib/python2.7/dist-packages/)
  5. 来自的目录 PYTHONPATH
  6. 中间路径(为简洁起见省略)
  7. userbase目录(~/.local/lib/python2.7/site-packages/)

我的问题是我需要在项目3和项目4之前放置项目5.以使我的代码正常工作.现在,如果我numpy 1.8.0/local/*目录导入一个编译过的模块,并且这个模块导入numpy,它仍然会从/usr/*目录中取出numpy 并失败.

我通过在我的脚本中放置这样的东西来规避这个问题:

import sys
sys.path.insert(0, '/local/python/lib/python2.7/site-packages/')
Run Code Online (Sandbox Code Playgroud)

因此,我可以强制Python使用正确的导入顺序,但当然这不是一个解决方案,因为我必须在每个脚本中 …

python module versions pythonpath sys.path

26
推荐指数
1
解决办法
2万
查看次数

MATLAB名称冲突:"错误的参数数量"

看来我的MATLAB名称与我的一个名为"annotation"的变量和内置的MATLAB函数"annotation"发生冲突.

在我的函数中,我正在加载包含变量注释的.mat文件,然后尝试将其用作另一个函数的参数.最小的工作示例如下所示:

function test()

    filenames = { 'file1.mat', 'file2.mat', 'file3.mat' };

    for i = 1:numel(filenames)
        in_file = char(filenames{i});
        out_file = strrep(in_file, '.mat', '_out.mat');

        prepare(out_file); % do something with the out file

        load(out_file);  % contains one variable named "annotation"
        which annotation % just to be sure

        other_function(annotation);
    end
end

function prepare(filename)
    annotation = rand(25, 1);
    save(filename);
end

function other_function(annotation)
    whos % just a stub - see whether it has been called
end
Run Code Online (Sandbox Code Playgroud)

现在,在我的函数准备中,我确保该文件包含一个名为"annotation"的变量.当我在main函数的循环中加载它时,"which"命令告诉我它作为变量存在,但在调用other_function时,MATLAB尝试调用函数"annotation":

注释是一个变量.

??? 在71处使用==>注释时出错

没有足够的输入参数

错误==> 14点测试

        other_function(annotation);
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我在程序的几个部分使用变量名"annotation",也作为函数调用中的参数.我能想象的唯一解释是MATLAB以某种方式预编译我的代码 …

matlab function

5
推荐指数
1
解决办法
2678
查看次数

tryCatch - 命名空间?

我对R很新,我对正确用法感到困惑tryCatch.我的目标是对大型数据集进行预测.如果预测无法适应内存,我想通过拆分数据来规避问题.

现在,我的代码大致如下:

tryCatch({
  large_vector = predict(model, large_data_frame)
}, error = function(e) { # I ran out of memory
  for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
    small_vector = predict(model, large_data_frame[i:(i+step-1), ])
    save(small_vector, tmpfile)
  }
  rm(large_data_frame) # free memory
  large_vector = NULL
  for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
    load(tmpfile)
    unlink(tmpfile)
    large_vector = c(large_vector, small_vector)
  }
})
Run Code Online (Sandbox Code Playgroud)

关键是如果没有错误发生,large_vector则按预期填充我的预测.如果发生错误,large_vector似乎只存在于错误代码的命名空间中 - 这是有道理的,因为我将其声明为函数.出于同样的原因,我收到一条警告说large_data_frame无法删除.

不幸的是,这种行为不是我想要的.我想large_vector …

r try-catch assign

4
推荐指数
1
解决办法
892
查看次数

标签 统计

assign ×1

function ×1

matlab ×1

module ×1

python ×1

pythonpath ×1

r ×1

sys.path ×1

try-catch ×1

versions ×1