6

I'm trying to follow this guide on pipenv and virtualenv: http://docs.python-guide.org/en/latest/dev/virtualenvs/ . The problem is, I run into a problem when trying to $ pipenv install requests (which in my case I think should be $python3 -m pipenv install requests since simply pipenv returns command not found.)

Why is permission denied?

I'm a terminal noob so bear with me.

$ pip3 install --user pipenv

$ python3 -m pipenv

Usage: __main__.py [OPTIONS] COMMAND [ARGS]...

$ python3 -m pipenv install requests

Creating a Pipfile for this project...
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/__main__.py", line 4, in <module>
    cli()
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/cli.py", line 895, in install
    ensure_project(three=three, python=python)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/cli.py", line 180, in ensure_project
    ensure_pipfile(validate=validate)
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/cli.py", line 141, in ensure_pipfile
    project.create_pipfile()
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/project.py", line 219, in create_pipfile
    self.write_toml(data, 'Pipfile')
  File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/project.py", line 226, in write_toml
    with open(path, 'w') as f:
PermissionError: [Errno 13] Permission denied: 'Pipfile'
5
  • try with sudo
    – voidpro
    Commented Sep 7, 2017 at 11:45
  • 12
    I've heard that that is bad practice.
    – Sahand
    Commented Sep 7, 2017 at 11:46
  • and have you tried with apt-get install python-YourPackage Commented Sep 7, 2017 at 11:48
  • @Sandi sudo runs the app in root mode. You can use sudo unless or otherwise you are aware of what you are doing. Otherwise, it may harm the core.
    – voidpro
    Commented Sep 7, 2017 at 11:51
  • 1
    I don't know what I'm doing, so it feels like a bad idea using sudo. If you think it's okay in this case I'd be glad to know why.
    – Sahand
    Commented Sep 7, 2017 at 11:53

2 Answers 2

7

Make sure you've added the UserBase's bin directory to your path (follow the Note box at the documentation you were following to see how to do this).

The third command you mentioned should just be: pipenv install requests.


Longer version:

I'll go a little more in depth for some of the command line concepts since you're getting started with the command line (and for others who would like a little more in depth reference).

You show three commands:

  • pip3 install --user pipenv
  • This is perfect, it installs pipenv as a user package (not avalible to the entire system)
  • python3 -m pipenv
  • This doesn't do anything. What you see returned is a "Usage message". It's saying this command expects some main.py program, options (optional because in parenthesis), a command (mandatory) and potentially more arguments. If you see a usage message it means you did not call that program the way its author intended.
  • python3 -m pipenv install requests
  • This should just be pipenv install requests. But it won't work until you've added UserBase's bin to your path (you'll get a pipenv: command not found error).

Your PATH is where your shell will search for the command you've listed. See changing your path on Mac or Linux or Windows.

As the documentation you were following mentions, you want to run python3 -m site, you'll get output something like the following:

$ python -m site
  .
  .
  .
USER_BASE: '/Users/<myusername>/Library/Python/3.6' (exists)
USER_SITE: '/Users/<myusername>/Library/Python/3.6/lib/python/site-packages' (exists)
ENABLE_USER_SITE: True

Now that you know where your USER_BASE is, add a /bin onto the end and add it to your PATH. Again see OS specific instructions but on OSX you can add export PATH="$PATH:/Users/<myusername>/Library/Python/3.6/bin to your ~/.profile, run source ~/.profile, and your shell will now search that directory when you enter the command pipenv.

1
  • Trying to follow this advice and got USER_BASE as follows: USER_BASE: '/home/<myusername>/.local' (exists) without mention of any /Library/Python/3.x. Is there a solution for those in this case? How do you get /Library/Python/3.x?
    – rom
    Commented Apr 17, 2022 at 15:44
3

I had the same symptoms:

with open(path, 'w') as f:
PermissionError: [Errno 13] Permission denied: 'Pipfile'

the problem was that I had a Pipfile in one of the parent directories and with different permissions (in my case, created by root while experimenting). Deleting this Pipfile resolved the issue. The same issue is discussed Pipenv issue.

More details: when trying to install/create a new virtual environment, pipenv looks into all the parent directories to see if there already is a Pipfile in there. If there is a Pipfile, and was created with different permissions (e.g. under root via sudo in my case), pipenv will not have permissions to write to this Pipfile, creating a somewhat unclear error message.

A tell tale sing is that pipenv --where prints a different directory name than you'd expect (or prints a directory path when you're just creating a new environment instead of "No pipfile present at project home...")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.