简体   繁体   English

我应该怎么做才能导入不是stdlib的math.py

[英]What should I do to import math.py that is not stdlib

I read the docs from python.org, but is still confused about this problem. 我从python.org阅读了文档,但是对此问题仍然感到困惑。 In one project, I have the following script structure: 在一个项目中,我具有以下脚本结构:

dir a: 目录A:

    math.py containing func c()
    main.py containing main()

What should I do in main.py in order to import math under dir a rather than stdlib? 为了在dir a而不是stdlib下导入数学,我应该在main.py中做什么?

import math just does not work 导入数学只是行不通

Edit: 编辑:

Sorry my mistake... This will never work. 对不起,我的错。 You can choose either to give your top level package a name that doesn't conflicts with a name in the standard library. 您可以选择为顶层软件包提供与标准库中的名称不冲突的名称。 Or the main script cannot be in the package directory. 否则主脚本不能在包目录中。 So basically you can either: 因此,基本上您可以:

Rename your module to my_math.py and then main.py can be in the same directory and you can just do: 将模块重命名为my_math.py ,然后main.py可以位于同一目录中,您可以执行以下操作:

from my_math import c
c()

Or you make a package, for example folder name test (or any other name that doesn't conflict with a standard library package) with files: __init__.py and math.py , and in the same level as the test you create a main.py and then the code will look like: 或者您使用文件__init__.pymath.py制作一个包,例如文件夹名称test (或与标准库包不冲突的任何其他名称),并在与test相同的级别上创建一个main.py ,然后代码将如下所示:

from test.math import c
c()

Folder structure: 资料夹结构:

.
|-- test
|   |-- __init__.py
|   `-- math.py
`-- main.py

This CAN work. 这可以工作。

sys.path.insert(0,'path/to/math.py')
import math

This will make python check at the specified directory BEFORE stdlibs. 这将使python在stdlibs之前的指定目录下进行检查。 This is a risky method for the same reason. 出于相同的原因,这是一种冒险的方法。 I would recommend putting math.py in its OWN directory and using that path, so nothing else is changed. 我建议将math.py放到其OWN目录中,并使用该路径,这样就不会更改其他内容。

I would also recommend importing this LAST if you have other modules imported as well. 如果您还导入了其他模块,我也建议导入LAST。

import stuff
import morestuff
sys.path.insert(0,'path/to/math')
import math

Again, using "math" is not recommended. 同样,不建议使用“数学”。 Renaming is best, especially for popular modules like "math". 重命名是最好的,特别是对于诸如“ math”之类的流行模块。

You can import modules with exact specification of the path to the module file, and by giving the module a specific name. 您可以导入模块,并指定模块文件路径的确切名称,并为模块指定一个特定的名称。 Have a look at the imp module. 看一下imp模块。

Here's an example: 这是一个例子:

# file: math.py
def say_hello():
    print "Hello!"

and the main file: 和主文件:

# file: main.py
import imp
my_math = imp.load_source("my_math", "math.py")
my_math.say_hello()

given both files lie where the current working directory is, this works for me. 鉴于这两个文件都位于当前工作目录所在的位置,因此这对我有用。

Note that this approach tends to be highly unmaintainable and unflexible. 请注意,这种方法往往难以维护且不灵活。 It is generally a good thing not to clash with standard library and other language feature names. 通常不要与标准库和其他语言功能名称冲突是一件好事。 Therefore, you should rename that module or move it to a (non-global) namespace, ie, a subpackage. 因此,您应该重命名该模块或将其移至(非全局)名称空间,即子包。

For reference: 以供参考:

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

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