简体   繁体   English

我的Python 3模块应该在哪里?

[英]Where should my Python 3 modules be?

I recently installed Python 3 on my Mac OSX 10.6.8 and I haven't had any problems with modules or imports until now. 我最近在Mac OSX 10.6.8上安装了Python 3,直到现在我还没有遇到任何模块或导入问题。 I'm writing a function that tests whether or not a triangle is right angled based on the length of the sides and the guide that the exercise was in has a bunch of equalities to test so I can see if it works: 我正在编写一个函数来测试三角形是否根据边的长度成直角,并且练习所在的指南有一堆相等的测试,所以我可以看看它是否有效:

testEqual(is_rightangled(1.5,2.0,2.5), True)
testEqual(is_rightangled(4.0,8.0,16.0), False)
testEqual(is_rightangled(4.1,8.2,9.1678787077), True)
testEqual(is_rightangled(4.1,8.2,9.16787), True)
testEqual(is_rightangled(4.1,8.2,9.168), False)
testEqual(is_rightangled(0.5,0.4,0.64031), True)

I should apparently import a function called testEqual(a,b,c) from a module called test, since the example programme in the guide starts with from test import testEqual , but when I typed that into my file I got this message: 我应该从一个名为test的模块中导入一个名为testEqual(a,b,c)的函数,因为指南中的示例程序from test import testEqual ,但是当我在我的文件中键入它时,我得到了这样的消息:

  from test import testEqual
ImportError: cannot import name testEqual

I suppose I should specify the path to the test module, but I can't find it my Python 3 library anywhere in my computer – just the 2.x ones that came installed with the computer, which are in /Library/Python . 我想我应该指定测试模块的路径,但我在我的计算机的任何地方都找不到我的Python 3库 - 只是随计算机安装的2.x版本,它们位于/Library/Python import turtle and import math worked, so it must be somewhere. import turtleimport math工作,所以它必须在某个地方。

The test module in the Python stdlib doesn't contain a function called testEqual() . Python stdlib中的test模块不包含名为testEqual()的函数。 Its documentation starts with 它的文档开头

Note: The test package is meant for internal use by Python only. 注意: test包仅供Python内部使用。 It is documented for the benefit of the core developers of Python. 它是为Python的核心开发人员的利益而记录的。 Any use of this package outside of Python's standard library is discouraged as code mentioned here can change or be removed without notice between releases of Python. 不鼓励在Python的标准库之外使用此包,因为此处提到的代码可以在Python版本之间更改或删除,恕不另行通知。

Are you sure that this guide you're following doesn't have its own test.py program that you're supposed to use instead? 您确定您所关注的本指南没有自己的test.py程序,而您应该使用它吗?

When you write your testEqual() function make note of the directory you are working in. For instance on my mac I created a directory (folder) in documents so my path looks like: /Users/myName/Documents/python. 当您编写testEqual()函数时,请记下您正在使用的目录。例如,在我的mac上,我在文档中创建了一个目录(文件夹),所以我的路径如下:/ Users / myName / Documents / python。 Save your function (module) as testEqual.py and when you write you test.py script import testEqual after the shebang line. 将您的函数(模块)保存为testEqual.py,并在shebang行之后编写test.py脚本导入testEqual。 Once you have your scripts debugged your modules will be in a folder that python creates titled pycache don't remove that as it is compiled code. 一旦你调试了你的脚本,你的模块将在python创建的文件夹中,标题为pycache不会删除它,因为它是编译代码。 Now, as long as you are working in the same dir as your module you should not need to do anything other than use the import statement. 现在,只要您使用与模块相同的目录,除了使用import语句之外,您不需要执行任何操作。

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

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