Excellent question. It gets to the heart of understanding how pipenv is designed to be used.
Calling pipenv run python main.py is not "bad" in the sense that it will fail or break your code. It works perfectly fine.
However, it's considered suboptimal, verbose, and less idiomatic for several key reasons. It indicates a slight misunderstanding of the convenience pipenv run is meant to provide.
Here’s the breakdown of why it's not the preferred method and what the better alternatives are.
Why pipenv run python main.py is Considered Suboptimal
1. It's Redundant and Verbose
The primary purpose of pipenv run <command> is to execute <command> within the context of the virtual environment. This means the environment's bin (or Scripts on Windows) directory is placed at the front of your PATH.
When you type pipenv run python ..., you are telling Pipenv:
- "Set up the environment for me."
- "Now, find the
python executable in that environment."
- "Use it to run my script."
The more idiomatic approach is to make your script directly executable, letting the system find the correct Python interpreter via the environment's PATH.
2. It Ignores the "Shebang" Convention
On Linux and macOS, the standard way to make a script executable is to:
- Add a shebang line at the very top of the file. This line tells the shell which interpreter to use to run the script. The most portable shebang for Python is:
#!/usr/bin/env python
- Make the script executable using the command:
chmod +x main.py
Once you do this, pipenv run ./main.py is all you need. The shell sees it's an executable, reads the shebang line, and pipenv run ensures that #!/usr/bin/env python resolves to the Python interpreter inside your virtual environment, not the system's global Python.
This is cleaner and more aligned with standard Unix practices.
3. It's Less Flexible
If you get into the habit of always typing python main.py, you might hardcode that into other scripts or documentation. The shebang approach is more flexible. If you were to switch to, say, a shell script that kicks off your Python app, the pipenv run ./my_script.sh pattern remains consistent.
The Recommended Alternatives
Here are the two preferred ways to run a script with Pipenv.
Method 1: The Idiomatic pipenv run (Best for CI/CD and one-off commands)
This is the method that uses the shebang.
1. Prepare your script (main.py):
Add the shebang line to the top and make sure the file is saved.
#!/usr/bin/env python
# main.py
import requests # A package from your Pipfile
print("Hello from inside the Pipenv environment!")
print(f"requests version: {requests.__version__}")
2. Make it executable (one-time setup):
In your terminal (on Linux/macOS):
chmod +x main.py
(This step is not necessary on Windows).
3. Run it:
Now you can run your script with this clean and simple command.
pipenv run ./main.py
Note: Using ./ is good practice to ensure you're running the script in the current directory.
Method 2: The Interactive pipenv shell (Best for development)
If you plan to run multiple commands, activating the virtual environment's shell is far more efficient.
1. Activate the shell:
pipenv shell
Your terminal prompt will change, indicating you are now "inside" the virtual environment. All commands you type will automatically use the tools and Python interpreter from that environment.
2. Run your commands naturally:
# No 'pipenv run' prefix needed!
python main.py
# You can also run other tools installed in the environment
pytest
black .
3. Deactivate the shell when done:
Simply type exit to return to your regular shell.
When is pipenv run python main.py Okay?
It's perfectly acceptable and even necessary in a few situations:
- On Windows: The shebang/
chmod +x concept doesn't apply in the same way. While pipenv run still works, pipenv run python main.py is a common and totally fine pattern.
- If the script is not executable: If you can't or don't want to add a shebang and
chmod +x, then pipenv run python main.py is your only option for a one-off command.
- For absolute clarity: For a beginner or in a tutorial,
pipenv run python main.py can be more explicit and easier to understand than the shebang mechanism.
Summary Table
| Command | Pros | Cons | Best For |
|---|
pipenv run python main.py | Works everywhere, very explicit. | Verbose, redundant, less idiomatic. | Quick one-off runs, Windows, scripts without a shebang. |
pipenv run ./main.py | Idiomatic, clean, follows Unix conventions. | Requires chmod +x setup (on Linux/macOS). | Automated scripts, CI/CD pipelines, one-off commands. |
pipenv shell then python main.py | Most efficient for development, no repetitive pipenv run. | Adds another shell layer, requires exit. | Interactive development sessions where you run many commands. |
Conclusion: While pipenv run python main.py isn't "bad," it's like using a screwdriver to hammer in a nail. It gets the job done, but it's not the intended tool for the task. Adopting pipenv run ./main.py or pipenv shell will lead to a cleaner, more standard, and more efficient workflow.