为什么从Python调用*.py文件时Python导入会成功,而通过文件名alon调用时却不会成功

2024-10-01 09:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正处于集成一些Python的早期阶段,以便在Matlab中调用一些计算,并将所有数据汇总到我将要使用的html生成中L.xist公司. 我最初安装的是32位的python2.7.5,但是在64位matlabr2015a安装中,我无法使用该版本Python的Matlab引擎。然后我下载了一个64位版本的Python2.7.9,并尝试在llxist中做一个pip,显然它没有得到很好的维护并且失败了。然后我下载了python3.4.364bit,下载了L.xist公司安装程序ll-xist-5.13.win-amd64-py3.4.exe并认为我可以走了。我不得不做一个小动作来拉腹肌,结果成功了。在

现在我要从控制台调用python文件了。如果我以python开头调用它,那么一切都很好,如果我不这样做,导入就无法识别。什么能解释这种差异呢?看起来在我安装的不同版本之间有一些安装障碍,不知怎么的,路径上的python版本没有被恰当地调用,我猜2.7.9版本的python正在被调用,因为这个安装从来没有被调用过L.xist公司安装了,但我的2.7.5安装了。在

C:\Temp>python MyScript.py a.txt b.txt
  file1: a.txt
  file2: b.txt

C:\Temp>MyScript.py a.txt b.txt
Traceback (most recent call last):
  File "C:\Temp\MyScript.py", line 20, in <module>
    from ll.xist    import xsc
ImportError: No module named ll.xist

这也是一个健全的检查。。。在

^{pr2}$

Tags: 数据py版本txthtml公司阶段temp
2条回答

检查.py的文件关联。虽然正确的安装可能在您的路径中,但文件关联可能仍指向其他版本。在

您可以通过在测试脚本中导入sys模块并调用print(sys.version)来检查这一点。然后用python test.py运行测试脚本,只运行test.py来查看打印的版本。在

原来这里的控制问题是Python Launcher for Windows.enter image description here

3.4. Python Launcher for Windows New in version 3.3.

The Python launcher for Windows is a utility which aids in the location and execution of different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version.

在我的控制台上,我可以通过py命令看到“默认”版本。。。在

C:\Users\me>py
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

C:\Users\me>py -3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

这是最基本的解决方法。。。在

The key benefit of this is that a single launcher can support multiple Python versions at the same time depending on the contents of the first line.

我要在我的剧本中添加的第一行是

^{pr2}$

伟大的评论链接here以了解发射器背后的细节和动机。在

相关问题 更多 >