How to Import Your Python Scripts with Anaconda on Mac and Windows
The Easiest Way to Import Python Scripts from Anywhere with Anaconda Without Having them Inside the Same Parent Folder or Altering the System Path.
Background
There are plenty of use cases for wanting to import your own Python scripts or packages for reuse in other programs. For instance, I frequently use this approach for importing usernames and passwords into scripts so I don’t have to display sensitive information. For instance, if I upload a script to GitHub, I don’t want anyone with access to the repository to know my passwords. This can easily be avoided by creating a .py file containing classes, the attributes of which are my usernames and passwords. I’ve mentioned this in other articles. Another use case is if you use Pandas to pull information out of a database and you use the same SQL queries on a regular basis. Simply create a function in a .py file and import the queries so you don’t have to rewrite, copy and past, or remember them every time you use them. There are many other applications of creating your own Python scripts or modules for reuse. With the method I’ll show you, you can import Python scripts from anywhere using Anaconda without having them inside the same parent folder or altering the system path.
Motivation
My primary motivation for writing this article is that I recently purchased an Apple Mac Mini after years of using a Windows machine for all my programming. I primarily use Anaconda because it’s great for data science and Conda is a convenient package manager. I’ve imported my own scripts on my Windows machines using Anaconda for a long time but when I purchased the Mac, I couldn’t figure out how to do it in the same way. This is largely because the instillation and file structure of Anaconda on Mac was markedly different from that of the Windows instillation. To make matters worse, when I tried researching how to do this on Mac I couldn’t find any good resources on how to do it easily. I decided to write this article to help other Anaconda users on Mac machines import their own Python scripts. Ok, sure… I’ll also show you how to do it on Windows as a bonus.
How To
Use Case:
For this article I’ll use the instance of a hypothetical developer on a team utilizing the Twitter API to gather data to do sentiment analysis etc. To use the API you’ll need to import your API Key and Secrets to pull data from Twitter. You certainly don’t want to put your password in shared code on GitHub for your whole team to see. Instead, you can create a script that contains your API Key and API Secret and you can import that into your work without anyone ever seeing them in plain text. One caveat here is if someone hacks your computer but that threat seems minimal and if it does happen, you have bigger things to worry about than the hacker getting your Twitter API password. This method does help minimize that risk though since you’ll only need to store your credentials in one spot and you can import them from anywhere rather than having the file in each of the project folders you use it for. Now that we have our scenario defined, I’ll walk you through the steps to set this up.
Step 1 Create a Directory to House Your Python Script
You’ll first need to create a directory. For this article, my directory is called creds/
and is placed on my desktop. The directory can be placed anywhere you’d like for now but you’ll want to have a folder created to place the Python files we’ll create in the subsequent steps. I’ll show you were to move the directory in Step 4.
Step 2 Create a Python Script for Import
The second thing you’ll need to do is create a Python script to import into another program. For this article we’re creating a file containing your Twitter API credentials called mycreds.py
inside the creds/
folder. Here’s how I created the file for my credentials.
As you can see, there are more credentials there than just for the Twitter API. Anyone who read my article about sending yourself a text message via email with Python so you know when your machine learning model is done training might recognize the use for the Google class.
Step 3 Create an __init__.py File
Inside your creds/
folder, create a file with nothing in it and save it as __init__.py
. This tells Python to treat your directory as a module so it can be imported. You can read more on this subject in the Python documentation.
Step 4 Move Your Directory
The next step is to move your creds/
directory where Anaconda will recognize it and you can import it into the script you’re trying to pull Twitter data with or whatever your actual use case is. I’ve included instructions on where to place it for Mac and Windows. Be sure to use the instructions for your particular OS.
Mac
On Mac, move your creds/
folder into:
Users/[your_username]/opt/anaconda3/lib/[python3.8]/site-packages/creds
Windows
On Windows, move your creds/
folder into:
C:\Users\[your_username]\Anaconda3\Lib\site-packages\creds
Reconciling for Your Machine
You’ll notice in each file path I’ve provided for both Mac and Windows, there are portions of the file path I put in square brackets. These portions of the file path are the parts of the file path you’ll likely need to change to fit your machine. For instance, in the Mac version of the file path, you may be running a different Python version in Anaconda. Simply change part of the path to your Python version. It could be python3.7
making the file path Users/[your_username]/opt/anaconda3/lib/python3.7/site-packages
for instance.
For both Mac and Windows, you’ll need to replace [your_username]
with your username. For example, if your name is Oscar D. Grouch, and you’re using Python 3.8 then the file path would be:
Mac: Users/oscardgrouch/opt/anaconda3/lib/python3.8/site-packages
Windows: C:\Users\oscardgrouch\Anaconda3\Lib\site-packages
Step 5 Use Your Module
Final step is to use your importable Python code. Again, now that it’s in the correct place you can import it from any other Python script on your machine regardless of what directory it’s located in.
As proof of concept, I created a Jupyter Notebook to use our mycreds.py file and put it in the following directory: C:\Users\michael\Google Drive\Documents\Medium\2021\Anaconda Import Package\Medium Import Example.ipynb
which is far far away from the folder we created the mycreds.py file located in C:\Users\michael\Anaconda3\Lib\site-packages
and the module still imports and runs perfectly.
Conclusion
In this article you’ve learned how to create a Python module that is easily imported into any script on your machine regardless of whether it’s Mac OS or Windows. You’ve also gotten a taste of what you can do with this knowledge now that you have it. I challenge you to think of other code you repeatedly write and put it into an importable module for future use. Use the DRY method. Don’t. Repeat. Yourself.
As always, let me know if you have any questions in the comments. Have a great day. Cheers!