I just finished writing up a quick script that defines 2 functions which convert Characters or Graphemes into Hexadecimal, and back (using the ocr() and chr() commands). The functions are both defined in one python file.
\MY probelm comes from the fact that I like to run python directly from the command line
Example:

Code:
chill@chillcakecalcsubuntu:~$ python
Python 2.7.12 (default, Jul  1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


If I open my program in IDLE and run it from there, then I can use the functions in the console after running the file. I want to know if there is any way to make the command line be able to open functions from the command line, similar to the console.

Basically, I want the command line to behave like the console in IDLE, or otherwise be able to use functions that are defined in external files.

Any help would be greatly appreciated. Thanks!
If your functions are defined in myfile.py, and you invoke python from the directory containing that file:
Code:
>>> from myfile import *

You can of course import only a subset of the module as well.

Code:
>>> from myfile import func1, func2
Tari wrote:
If your functions are defined in myfile.py, and you invoke python from the directory containing that file:
Code:
>>> from myfile import *

Yes, I do remember that. After further thought, I see that if you point the terminal, using cd, to the folder containing the python files themselves, then the imports work seamlessly. Thanks for your help.
CHill wrote:
Tari wrote:
If your functions are defined in myfile.py, and you invoke python from the directory containing that file:
Code:
>>> from myfile import *

Yes, I do remember that, but how do I point the 'from' command to the correct file on my disk? Or, is there a certain folder I should place import files?
When I open up a file in the same directory as that file, I know that I can import the module into that file, but If I want to import the functions into the python being run out of the command line.


Just make sure you are in the same folder as your file when you run python.
Other relevant tips:

The -i option will evaluate things as usual then drop to the REPL, so you can do python -i /path/to/my/script.py and not have to worry about paths. I think this is the best option (which I just learned about too!), and it's probably what IDLE uses under the hood.

You can set sys.path to control where Python looks for things to import:
Code:
import sys, os.path
sys.path.append(os.path.dirname("/path/to/my/script.py"))
import script
Or set it via the environment, which is handy if you're continually relaunching interpreters.
Code:
$ export PYTHONPATH=/path/to/my
$ python
>>> import script


Unless you're just experimenting, I'd recommend using somewhat more principled ways to see what you program is doing and verify its operations, be that automated testing, getting friendly with the debugger, or just making the thing a script so you can invoke functions from the command line more easily.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement