简体   繁体   中英

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. 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. I'm wondering if the path that python is searching is different, because of the following:

After the install, I see this message 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

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 .

To access the functions in story.py you need to import the module at the top of another python file, or in the interpreter.

If story.py contained

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 . 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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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