Skip to content
Advertisement

Can’t import my own modules in Python

I’m having a hard time understanding how module importing works in Python (I’ve never done it in any other language before either).

Let’s say I have:

JavaScript

Now I’m trying to get something like this:

JavaScript

However, I’m definitely doing something wrong as Python can’t see that myapp is a module:

JavaScript

Advertisement

Answer

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

JavaScript

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

JavaScript

However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:

JavaScript

though that is generally not recommended.

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement