Unable to import my own modules in Python

It's hard for me to understand how importing modules into Python works (I never did this in any other language before it was).

Let's say I have:

myapp/__init__.py myapp/myapp/myapp.py myapp/myapp/SomeObject.py myapp/tests/TestCase.py 

Now I am trying to get something like this:

 myapp.py =================== from myapp import SomeObject # stuff ... TestCase.py =================== from myapp import SomeObject # some tests on SomeObject 

However, I'm definitely doing something wrong, since Python does not see that myapp is a module:

 ImportError: No module named myapp 
+109
python import module package
Feb 21 '12 at 18:27
source share
8 answers

In your particular case, it looks like you are trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py do

 import SomeObject 

since it is in the same folder. For TestCase.py do

 from ..myapp import SomeObject 

However, this will only work if you import TestCase from the package. If you want to run python TestCase.py directly, you have to get confused with your path. This can be done in Python:

 import sys sys.path.append("..") from myapp import SomeObject 

although this is usually not recommended.

In general, if you want other people to use your Python package, you should use distutils to create a script setup. That way, anyone can easily install your package with a command like python setup.py install , and it will be available everywhere on their machine. If you are serious about a package, you can even add it to the Python package index, PyPI .

+79
Feb 21 '12 at 18:46
source share

The import function searches for files in your PYTHONPATH env. variable and your local directory. Thus, you can either put all your files in one directory or export the path that enters the terminal:

 export PYTHONPATH="$PYTHONPATH:/path_to_myapp/myapp/myapp/" 
+30
Feb 21 '12 at 18:31
source share

export way is a good way. Another way is to add .pth to the location of your site. On my mac, my python stores the site packages in / Library / Python shown below

 /Library/Python/2.7/site-packages 

I created the awesome.pth file in / Library / Python / 2.7 / site-packages / awesome.pth, and put the following path in the file that references my amazing modules

 /opt/awesome/custom_python_modules 
+10
May 20 '13 at 23:36
source share

You can try

 from myapp.myapp import SomeObject 

because the name of your project matches the name myapp.py, which first searches for the project document

+6
Mar 06 '17 at 13:54 on
source share

In your first myapp directory, you can add setup.py file and add two python codes to setup.py

 from setuptools import setup setup(name='myapp') 

in your first myapp directory on the command line, use pip install -e. to install the package

+2
Oct 28 '17 at 11:30
source share

pip install in Windows 10 is installed by default in "Program Files / PythonXX / Lib / site-packages", which is a directory requiring administrative privileges. Therefore, I fixed the problem by running pip install as administrator (you must open the command line as administrator, even if you are logged in with an administrator account). It is also safer to call pip from python.
eg
python -m pip install <package-name>
instead
pip install <package-name>

+2
Aug 21 '18 at 9:12
source share

In my case, it was a surprise of Windows against Python, although the file names on Windows are not case sensitive, importing from Python. So if you have a Stuff.py file you need to import that name as it is.

0
May 17 '18 at 9:31
source share

Try to run the program of the main file in which the function is created.

0
Jun 28 '19 at 12:36 on
source share



All Articles