Repository: pypa/pipenv

Issue:
PipfileNotFound raised for no Pipfile while installing
from the previous version, when there is no Pipfile, one will be automatically created when executing pipenv install.
However, after 2018.11.14, an error is thrown
Pipfile not found! Aborting! Please ensure that the file exists and is located in your project root directory.

### Steps to replicate
1, pipenv install


Discussion:
Hey @nealyip,

We did a quick check and this issue looks very darn similar to

* [#3259 - Update pythonfinder for resilient parsing](3259)
* [#3255 - Installation failed due to missing hashes when using --skip-lock](3255)
* [#3252 - Updated graph command docstrings, fixes #3071](3252)

This could be a coincidence, but if any of these issues solves your problem then I did a good job :smile:

If not, the maintainers will get to this issue shortly.

Cheers,
Your Friendly Neighborhood ProBot

`pipenv install` without package names is disallowed when there is no Pipfile present.

Any of the following is OK to initialize a pipenv project:
```
$ pipenv --python python
$ pipenv --two/--three
$ touch Pipfile && pipenv install
```
@nealyip @frostming I have this same issue. Wanted to file an issue today at 8 am but could not. This time I searched again for duplicates. And almost missed this. Had to read it to realise I had the same issue.

Use Case:
Use `pipenv install` to create a fresh env as its faster than `pipenv install x`. Then  use `pipenv shell`  to enter the env and setup the env.

Steps to repro+ error:
[pipenv-WYDT.txt](https://github.com/pypa/pipenv/files/2599939/pipenv-WYDT.txt)

Expected:
pipenv installs a new env with python, pip, setuptools,  and wheel. And then you can install packages without pipenv slowing you down.

Details:-
**OS Specs:** Edition->Windows 10 Pro, 
Version->1803, 
Build->17134.407
**Device Specs:** System type->64-bit OS, x64-based processor, 
Installed RAM: 16.0GB(15.9GB usable), 
Processor: Intel Core i5-7400 CPU

Python: 3.7.1 
Pipenv: 2018.11.14
WinPython: 3.7.1.0-64bit 

Moreover-
WinPython is not registered as system Python. Python is not on user or system Path. 
No other Python version is installed on the system.
@nealyip 
Reply to:- [https://github.com/pypa/pipenv/issues/3260#issuecomment-440193629](url)
> `pipenv install` without package names is disallowed when there is no Pipfile present.

Since when? I really don't know anything about this and I tried to. Can you please give a link?

**Note( for Windows 10 1803 users): The top two methods have been tested succesfully on Windows 10  @1803!**
1. > pipenv --python python

Good to know. Thanks.

2. > pipenv --two/--three

Good to know. Thanks. Also, this can be added to the error that suddenly appeared.

3. >  touch Pipfile && pipenv install

I am on windows. This causes error in Powershell and cmd. `touch Pipfile`  followed by `pipenv install` also doesn't work. 
So, I used Notepad++ to create Pipfile. After I copy a Pipfile from the other env, edit it and paste it in the current directory, it works!

_PS:- I hope this and the above comment have been helpful._
@frostming Is there any chance this will get fixed? If yes, thanks a lot. I would like to help.

If no, thanks to all contributors for pipenv, it was like salt( is to food). I'll just have to stop using pipenv and go back to using virtualenv. I can't change my workflow. I would still like to help, but I am not sure if its okay to contribute to projects I don't use often. Am I mistaken?
I just reviewed the original issue linked above and find it was a bad fix to change the behavior. You are right, `pipenv install` should work to create the Pipfile correctly.

I will submit a fix soon. Thank you.
@snow8gaia I'm also interested in this comment specifically:

>  WinPython is not registered as system Python. Python is not on user or system Path.

can you run `pipenv --support` and provide the output?
Also in retrospect, there was no api change introduced and no flaw in the exception handling, there is just a bug in pipfile generation which results in the downstream exception which tells you there is no pipfile.

Fix will be up in a sec

Resolving patch:
diff --git a/pipenv/core.py b/pipenv/core.py
--- a/pipenv/core.py
+++ b/pipenv/core.py
@@ -1712,14 +1712,8 @@ def do_install(
     # Don't search for requirements.txt files if the user provides one
     if requirements or package_args or project.pipfile_exists:
         skip_requirements = True
-    # Don't attempt to install develop and default packages if Pipfile is missing
-    if not project.pipfile_exists and not (package_args or dev) and not code:
-        if not (ignore_pipfile or deploy):
-            raise exceptions.PipfileNotFound(project.path_to("Pipfile"))
-        elif ((skip_lock and deploy) or ignore_pipfile) and not project.lockfile_exists:
-            raise exceptions.LockfileNotFound(project.path_to("Pipfile.lock"))
     concurrent = not sequential
-    # Ensure that virtualenv is available.
+    # Ensure that virtualenv is available and pipfile are available
     ensure_project(
         three=three,
         python=python,
@@ -1729,6 +1723,12 @@ def do_install(
         skip_requirements=skip_requirements,
         pypi_mirror=pypi_mirror,
     )
+    # Don't attempt to install develop and default packages if Pipfile is missing
+    if not project.pipfile_exists and not (package_args or dev) and not code:
+        if not (ignore_pipfile or deploy):
+            raise exceptions.PipfileNotFound(project.path_to("Pipfile"))
+        elif ((skip_lock and deploy) or ignore_pipfile) and not project.lockfile_exists:
+            raise exceptions.LockfileNotFound(project.path_to("Pipfile.lock"))
     # Load the --pre settings from the Pipfile.
     if not pre:
         pre = project.settings.get("allow_prereleases")
diff --git a/pipenv/project.py b/pipenv/project.py
--- a/pipenv/project.py
+++ b/pipenv/project.py
@@ -693,6 +693,7 @@ def create_pipfile(self, python=None):
             ConfigOptionParser, make_option_group, index_group
         )
 
+        name = self.name if self.name is not None else "Pipfile"
         config_parser = ConfigOptionParser(name=self.name)
         config_parser.add_option_group(make_option_group(index_group, config_parser))
         install = config_parser.option_groups[0]


Test patch:
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -223,13 +223,14 @@ def __exit__(self, *args):
         os.umask(self.original_umask)
 
     def pipenv(self, cmd, block=True):
-        if self.pipfile_path:
+        if self.pipfile_path and os.path.isfile(self.pipfile_path):
             os.environ['PIPENV_PIPFILE'] = fs_str(self.pipfile_path)
         # a bit of a hack to make sure the virtualenv is created
 
         with TemporaryDirectory(prefix='pipenv-', suffix='-cache') as tempdir:
             os.environ['PIPENV_CACHE_DIR'] = fs_str(tempdir.name)
-            c = delegator.run('pipenv {0}'.format(cmd), block=block)
+            c = delegator.run('pipenv {0}'.format(cmd), block=block,
+                              cwd=os.path.abspath(self.path))
             if 'PIPENV_CACHE_DIR' in os.environ:
                 del os.environ['PIPENV_CACHE_DIR']
 
diff --git a/tests/integration/test_install_basic.py b/tests/integration/test_install_basic.py
--- a/tests/integration/test_install_basic.py
+++ b/tests/integration/test_install_basic.py
@@ -418,3 +418,16 @@ def test_system_and_deploy_work(PipenvInstance, pypi):
         )
         c = p.pipenv("install --system")
         assert c.return_code == 0
+
+
+@pytest.mark.install
+def test_install_creates_pipfile(PipenvInstance):
+    with PipenvInstance(chdir=True) as p:
+        if os.path.isfile(p.pipfile_path):
+            os.unlink(p.pipfile_path)
+        if "PIPENV_PIPFILE" in os.environ:
+            del os.environ["PIPENV_PIPFILE"]
+        assert not os.path.isfile(p.pipfile_path)
+        c = p.pipenv("install")
+        assert c.return_code == 0
+        assert os.path.isfile(p.pipfile_path)