Comments (2)
Great question! Here is the short answer to your specific question:
- Assuming that your terminal's current path is at
mod/
, you can runpython -m unittest tests/test_module.py
. - If your terminal's current path is anywhere else, you will see errors. For example, if your terminal's current path is at
mod/tests/
, then runningpython -m unittest test_module.py
will fail.
Here's the longer explanation. The directory structure you described is as follows:
mod/
module.py
tests/
test_module.py (contains "import module")
The usage of the -m
flag for the Python interpreter is something I did not really address in my guide, because it introduces some of its own complications. Below is a quick primer.
Suppose that the terminal is currently at some directory dir/
. Then, the following command starts a Python interpreter with sys.path[0]
set to the empty string ""
which refers to the current path /path/to/dir/
.
/path/to/dir/> python -m <module_name>
Therefore, in case 1 described above (/path/to/mod/> python -m unittest tests/test_module.py
), the mod/
directory is in sys.path
, so test_module.py
is able to successfully call import module
.
However, in case 2 (/path/to/mod/tests/> python -m unittest test_module.py
), mod/
is not in sys.path
, so the call to import module
fails.
from chrisyeh96.github.io.
Thanks a lot @chrisyeh96 for the thorough response! Makes more sense now.
from chrisyeh96.github.io.
Related Issues (8)
- Recap order in which Python searches for modules to import HOT 1
- Possible Typo HOT 1
- Solution 1 to Case 2 seems to work for Case 3, too HOT 2
- Cannot replicate your page about LQR HOT 1
- module import precedence mistake in the Python imports article HOT 2
- Syntax error for pkgutil.iter_modules use case in The Definitive Guide to Python import Statements post HOT 2
- Navbar spacing HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrisyeh96.github.io.