Dear Harrison, I have a git repository. Inside the repository, I created multiple project folders for storing different projects. I have two projects sharing some commonality. Because of that, I created a module (let's call it theCommonModule) that can be used to do data manipulation for both projects. This module is saved in the root repository.
So the structure is like this:
Root repo -> sub folder for project 1 (including main1.py) -> sub folder for project 2 (including main2.py) -> theCommonModule.py
The code in main1.py is as below:
import theCommonModule import pandas import numpy
some code here
Python returned an error: can not find theCommonModule
I think I got the error is because theCommonModule is not saved under the current directory. I am not sure what is the best way to solve this problem. The only dodgy way i can think of is to save theCommonModule in both sub folders. I was wondering if you could shed some light on the problem?
Thank you so much!
Rachel
You must be logged in to post. Please login or register an account.
If you're able to, you don't want to import backwards like that, you want you main functions to only import further nested modules, not go backwards to parent modules.
If you are going to use multiple project dirs like that, then my suggestion is to, rather than having theCommonModule locally, put it in your site-packages.
On Windows, find where Python is installed. For me, that's C:/Python35. From there, find /lib/site-packages/ and put your theCommonModule there.
Then you can import it from anywhere.
You can also put a directory here, where the directory name is the package name and the python scripts inside there can be imported from it.
-Harrison 8 years ago
You must be logged in to post. Please login or register an account.
Thank you so much for you reply! I am not sure about what you mean for the last point "You can also put a directory here, where the directory name is the package name and the python scripts inside there can be imported from it. "
Could you please rephrase or give me an example?
Thank you so much!!!
Rachel
-rachelhuang 8 years ago
You must be logged in to post. Please login or register an account.
I think he is talking about if you had multiple modules, then you want to put them in as a package in their own directory within your site-packages/ directory. Then import them like
from my_package import my_module
. But since you just have one module, I would just stick it in the site-packages/ directory and not worry about making it more complicated. http://stackoverflow.com/questions/15746675/how-to-write-a-python-module
-Dave31 8 years ago
You must be logged in to post. Please login or register an account.