简体   繁体   English

python的路径与我的.bash_profile不同

[英]Path for python is different than my .bash_profile

I'm working through Learn Python the Hard way, and I'm currently on exercise 46 where you learn to create packages. 我正在努力学习Python,目前正在练习46中,您将学习如何创建软件包。 I've created a basic package that does a few calculations, and uses a few different modules. 我创建了一个基本的程序包,该程序包进行了一些计算并使用了一些不同的模块。

I've gotten the package to install in my python2.7 site packages, but I can't seem to run the module from my site packages after the fact. 我已经将该软件包安装在我的python2.7网站软件包中,但是事发之后,我似乎无法从我的网站软件包中运行该模块。 I'm wondering if the path that python is searching is different, because of the following: 我想知道python搜索的路径是否不同,原因如下:

After the install, I see this message Copying story-0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages 安装后,我看到此消息, Copying story-0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages

However, when I try to run the module, I see this message /usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'story.py': [Errno 2] No such file or directory 但是,当我尝试运行该模块时,会看到以下消息/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'story.py': [Errno 2] No such file or directory

Sorry if this makes absolutely no sense, I'm very new to the fantastical world of programing. 抱歉,如果这绝对没有意义,那么我对梦幻般的编程世界是陌生的。

When you install a package, you access the internals differently. 安装软件包时,您以不同的方式访问内部。 You can no longer just call python story.py . 您不再只能调用python story.py

To access the functions in story.py you need to import the module at the top of another python file, or in the interpreter. 要访问story.py的功能,您需要将模块import另一个python文件顶部或解释器中。

If story.py contained 如果包含story.py

def my_test_function(blah):
    print blah

You would use this function in another file by the following (after installing the module, as you've already done) 您可以通过以下方法在另一个文件中使用此功能(在安装模块之后,您已经完成此操作)

import story

story.my_test_function("Hello!")

By importing the module, you get access to all the functions and classes inside it by typing module_name.function_name . 通过导入模块,您可以通过输入module_name.function_name来访问其中的所有函数和类。 You could also just import the function you wanted to use directly, which would look something like 您也可以只导入要直接使用的功能,看起来像

from story import my_test_function

my_test_function("Hello!")

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

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