1

I am trying to fill a requirements.txt for my PyCharm Django project that has a virtual environment. I am using the following when my virtual environment has been started:

pip freeze > requirements.txt

This creates lots of entries as shown here but does not have entries for packages I have added such as djangorestframework, Django-crispy-forms etc.

What do I have to do to get all my packages listed? None of the packages installed within the virtual environment venv/lib/python 3.6/site-packages are listed in the requirements.txt

At the moment my requirements.txt looks like this:

altgraph==0.10.2
amqp==2.1.4
appdirs==1.4.3
awsebcli==3.10.0
backports.ssl-match-hostname==3.5.0.1
bdist-mpkg==0.5.0
billiard==3.5.0.2
blessed==1.14.1
bonjour-py==0.3
botocore==1.5.26
cement==2.8.2
click==6.7
colorama==0.3.7
coreapi==2.3.0
coreschema==0.0.4
defusedxml==0.4.1
django-appconf==1.0.2
dockerpty==0.4.1
funcsigs==1.0.2
futures==3.1.1
itypes==1.1.0
Jinja2==2.9.6
jmespath==0.9.2
kombu==4.0.2
macholib==1.5.1
MarkupSafe==1.0
matplotlib==1.3.1
modulegraph==0.10.4
numpy==1.8.0rc1
oauthlib==2.0.1
olefile==0.44
openapi-codec==1.3.1
packaging==16.8
pathspec==0.5.0
pbr==3.0.1
progressbar2==3.20.0
psycopg2==2.7.3.2
py2app==0.7.3
PyJWT==1.5.0
pyobjc-core==2.5.1
pyobjc-framework-Accounts==2.5.1
pyobjc-framework-AddressBook==2.5.1
pyobjc-framework-AppleScriptKit==2.5.1
pyobjc-framework-AppleScriptObjC==2.5.1
pyobjc-framework-Automator==2.5.1
pyobjc-framework-CFNetwork==2.5.1
pyobjc-framework-Cocoa==2.5.1
pyobjc-framework-Collaboration==2.5.1
pyobjc-framework-CoreData==2.5.1
pyobjc-framework-CoreLocation==2.5.1
pyobjc-framework-CoreText==2.5.1
pyobjc-framework-DictionaryServices==2.5.1
pyobjc-framework-EventKit==2.5.1
pyobjc-framework-ExceptionHandling==2.5.1
pyobjc-framework-FSEvents==2.5.1
pyobjc-framework-InputMethodKit==2.5.1
pyobjc-framework-InstallerPlugins==2.5.1
pyobjc-framework-InstantMessage==2.5.1
pyobjc-framework-LatentSemanticMapping==2.5.1
pyobjc-framework-LaunchServices==2.5.1
pyobjc-framework-Message==2.5.1
pyobjc-framework-OpenDirectory==2.5.1
pyobjc-framework-PreferencePanes==2.5.1
pyobjc-framework-PubSub==2.5.1
pyobjc-framework-QTKit==2.5.1
pyobjc-framework-Quartz==2.5.1
pyobjc-framework-ScreenSaver==2.5.1
pyobjc-framework-ScriptingBridge==2.5.1
pyobjc-framework-SearchKit==2.5.1
pyobjc-framework-ServiceManagement==2.5.1
pyobjc-framework-Social==2.5.1
pyobjc-framework-SyncServices==2.5.1
pyobjc-framework-SystemConfiguration==2.5.1
pyobjc-framework-WebKit==2.5.1
pyOpenSSL==0.13.1
pyparsing==2.2.0
python-dateutil==1.5
python-openid==2.2.5
pytz==2013.7
PyYAML==3.12
requests-oauthlib==0.8.0
scipy==0.13.0b1
semantic-version==2.5.0
simplejson==3.10.0
six==1.10.0
social-auth-app-django==1.2.0
social-auth-core==1.3.0
sqlparse==0.2.3
tabulate==0.7.5
termcolor==1.1.0
uritemplate==3.0.0
vboxapi==1.0
vine==1.1.3
virtualenv==15.1.0
wcwidth==0.1.7
websocket-client==0.40.0
xattr==0.6.4
zope.interface==4.1.1
Bill Noble
  • 6,466
  • 19
  • 74
  • 133
  • 1
    activate the virtualenv first – Sayse Nov 11 '17 at 10:56
  • I have run 'source venv/bin/activate' to activate my virtual environment and run 'pip freeze' but it doesn't put the packages in the requirements.txt file. I get the same as before. – Bill Noble Nov 11 '17 at 11:19
  • probably some if these packages are installed globally? – Alexander Tyapkov Nov 11 '17 at 12:01
  • The packages are installed within the virtual environment venv/lib/python 3.6/site-packages. None of these packages are listed in the requirements.txt – Bill Noble Nov 11 '17 at 13:09
  • if You are using one of *nix system could You add output from command `which pip` ? – Take_Care_ Nov 13 '17 at 21:40
  • @Take_Care_ which pip gives me: /Users/billnoble/.pyenv/shims/pip I am running on a Mac – Bill Noble Nov 13 '17 at 21:44
  • You need to call venv/bin/pip, the pip version installed in your virt environment. Also you could try pip3 – Sabir Akhadov Nov 14 '17 at 07:03
  • @SabirAkhadov there is no pip in vent/bin/pip – Bill Noble Nov 14 '17 at 08:05
  • This looks like your virtual env is not properly setup. I would suggest you use `virtualenvwrapper` instead. See this link https://virtualenvwrapper.readthedocs.io/en/latest/install.html and install virtualenv wrapper as per the document. Then use `mkproject ` to create the project. You can use the variables `WORKON_HOME` and `PROJECT_HOME` to let it load existing virtual environments – Tarun Lalwani Nov 14 '17 at 13:54
  • @TarunLalwani I have a working virtual environment at venv/ and am trying to get a requirements.txt for this – Bill Noble Nov 14 '17 at 16:46
  • Are you installing the packages via PyCharm? If so, there is a checkbox for "Install to user's site packages directory". Is this checked or unchecked? – nanotek Nov 14 '17 at 16:56

1 Answers1

3

EDIT:

Since the follow up in the comments, we have reached the conclusion that the virtual environment that @BillNoble created didn't have pip installed in it and that was what caused the problem.

By following this post: virtualenv does not include pip he was able to solve it.

I will leave my previous answer here because it is the answer to the initial phrasing of the question.


Pycharm Version 2017.2:

  1. Go to: File > Settings > Project: > Project interpreter
  2. In there, hit the button with the cog symbol.
    If you have a virtual environment, choose "add local" (3).
    If you don't have a virtual environment, choose "create VirtualEnv" (4).
  3. Add local: Navigate to your virtual environments folder and find your existing one. Then choose: path/to/venv /my_venv/bin/pythonX.X
  4. Create VirtualEnv: Just give it a name and select the python version you want.
  5. Go to Pycharm terminal and activate your virtual env (or restart pycharm).

(EDIT: added after Pycharm update) Pycharm Version 2017.3.3:

  1. Go to: File > Settings > Project: > Project interpreter
  2. In there, hit the button with the cog symbol and choose "Add Local":
    If you have a virtual environment, choose "Existing environment" (3).
    If you don't have a virtual environment, choose "New environment" (4).
  3. Existing environment: In the "Interpreter" tab, navigate to your virtual environments folder and find your existing one. Then choose: path/to/venv /my_venv/bin/pythonX.X
  4. New environment: Define the location of the virtual environment in the "Location" tab (usually home/username/.virtualenvs/your_venv) and choose the base interpreter from the drop-down menu.
  5. Go to Pycharm terminal and activate your virtual env (or restart pycharm).

Now install anything you need and when you do a pip freeze > requirements.txt you will get everything installed inside your virtual env.

Keep in mind that you don't need everything from your venv inside your requirement.txt. Most of the modules in there are dependencies of other modules (like Django) and the correct version of them will get installed if you try to install the depended package.

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • @BillNoble Then follow step 3 without step 4 – John Moutafis Nov 14 '17 at 12:35
  • I have tried that but pip freeze is still not listing the Django packages installed in the virtual environment – Bill Noble Nov 14 '17 at 16:43
  • @BillNoble When you call `pip freeze` without the `> requirements.txt` part, do you see the Django module? – John Moutafis Nov 14 '17 at 18:00
  • No I just see what is listed in my question – Bill Noble Nov 14 '17 at 19:01
  • @BillNoble Try to reinstall Django (`pip install django`) and then `pip freeze` again. Do you see it now? – John Moutafis Nov 14 '17 at 19:03
  • If I run pip install from the command line where my virtual environment is active it is trying to install Django in the wrong place. I get this error: OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django' – Bill Noble Nov 14 '17 at 19:18
  • @BillNoble Did you create your virtual env using `sudo`? Anyway, try to create a new virtual env (preferably if you are a new user, try creating it via Pycharm) and retry your installation. – John Moutafis Nov 14 '17 at 19:36
  • The virtual environment was created by PyCharm and activate by 'source vent/bin/activate – Bill Noble Nov 14 '17 at 19:38
  • There is no pip in venv/bin – Bill Noble Nov 14 '17 at 19:38
  • @BillNoble try something like this then: https://stackoverflow.com/questions/34503686/virtualenv-does-not-include-pip – John Moutafis Nov 14 '17 at 19:49
  • I don't have a pydistutils.cfg file so I am not sure how that helps. Is there a straightforward way to install pip in my virtual environment? – Bill Noble Nov 14 '17 at 19:59
  • I have managed to Install pip in my virtual environment and now I get the correct contents for my requirements.txt. If you would like to update your answer to reflect this I will award the points to you. – Bill Noble Nov 14 '17 at 20:28