This document has notes on how to make a Python ".py" file accessible to your code assuming an Anaconda/iPython distribution. The content was researched for and tested on a Windows 7 64-bit machine with an Anaconda distribution of Python 2.7. These instructions are written with Windows and Macintosh in mind, but some testing may be required to find the best solution on your system.
The ".py" file is assumed to contain functions or reusable object code. Note that for production content where you will re-use the content across a lot of code, you should look up actually packaging your own libraries. This is recommended more for testing reusable code because it is quicker and easier than package installation and setup. A link is provided in the reference links below to how to make packages for production ready code.
Reference URLs:
SO = StackOverflow
In [8]:
import sys
sys.path.append(r"C:\ProgramFilesCoders")
# replace path with your path
# use r"" around windows paths so \ is treated as raw and does not have to be escaped
Making this permanent is explained in the next section of this notebook. Once these commands are run, you can import your file like this:
import myFile as mFmF.myFunction()In some ways this content is experimental. According to help documentation, you can modify a configuration file to have the commands shown above and this is supposed to make the effect permanent. Topics provided as hyperlinks at the start of this notebook also recommend placing a file at the start of each folder to enable them for access and make their subfolders accessible. The file to place in each folder is named __init__.py. This file can be empty, or it can contain commands that are supposed to run everytime you import from the folder:
The configuration file to modify is:
c:\your_user__path\.ipython\profile_default\ipython_config.py.
By default, all lines in this file are commented out. Uncomment this line and modify the commands stored in the list as shown here:
c.InteractiveShellApp.exec_lines = ['import sys; sys.path.append(r"C:\your_code_path\your_module_folder")]
If you have more than one path to add, add in multiple sys.path.append() lines separated by semi-colons (;) as shown above. Note that r"" treats the path as raw text so that the backslashes in the windows path are not confused for escape characters.
In testing this on a windows 7 64 bit machine, the paths did not become effective until they were added to the windows user environment variable "PYTHONPATH". To do this, each path was added to the environment variable separated by the semicolon (;). On Windows 7, this accessed through:
Control Panel --> System and Security --> System --> Advanced System Settings --> Environment Variables --> User section --> PYTHONPATH
If it does not exist, create it. If it does exist, edit it and add your paths separated by ; to the end of any paths currently contained in the variable.
It is not known as of this writing if it was just the environment variable or the combination of setting up the ipython_config.py and setting up the environment variables that got the code to work
The cells that follow are tests of some functions stored in the python module files referenced in the coding cells. To run these tests on your system, get the files from this project, put them in a folder, set up your environment as described here and then you can run these cells to test that you got this process working.
In [10]:
# a quick demo with one of the files from this folder:
# on github, the source is at: DataTech_Playground\Python_Misc
# on TMWP local machine, installed to folder path c: ... \Python_Misc and configured for permanent access
import TMWP_string_and_array_functions as strAndArray # install .py file or this does not work
dir(strAndArray) # get list of what functions and variables are in the module
Out[10]:
In [12]:
print strAndArray.version
print strAndArray.python_version_support
print strAndArray.initialCaps_anyString("this IS my TEST string that will convert to camel case.")
In [ ]: