在使用conda环境时如何导入本地代码?
我在某些位置有一些代码,例如C:\ Users \ me \ Code \ mycode \,并且该目录在我的path变量中。从基本的python环境中,我可以导入它:
> python --version
Python 3.7.3
> python
>>> import mycode
>>> #worked
但是,如果我激活了conda环境,则无法再访问我的代码。
> conda activate myenv
(myenv) > python
>>> import mycode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mycode'
是否有一种方法告诉conda启用path变量中包含的区域,或轻松地将其他本地代码包含到环境中?
答案 0 :(得分:0)
Python使用the PYTHONPATH
variable填充用于搜索模块的路径。您可以使用#nullable enable
using System.Diagnostics.CodeAnalysis;
public sealed class MyOptions
{
[DisallowNull]
public string? Name { get; set; }
public static void Test()
{
var options = new MyOptions();
options.Name = null; // warning
options.Name = "Hello"; // ok
}
public static void Test2()
{
var options = new MyOptions();
options.Name.Substring(1); // warning on dereference
}
}
检查Python在哪里搜索。
我建议仅少量使用。这有点违反了Conda envs试图实现的隔离。就个人而言,我将避免在全局级别设置此变量,而应仅在env级别设置它。当前为this can be done with activation hooks,但在Conda v4.8中,您可以使用the conda env config vars
command在命令行中进行设置。