Can pycharm create the requirements.txt file - what am I missing?

Answered

Sorry if this seems like it should be so very obvious - I am new to python so your input would be appreciated.

I have a Flask application that I develop with pycharm on a mac/pc - then I want to install it on a raspberry pi and I would like to generate a requirements.txt from the virtual environment that I setup in pycharm.  I dont see how to auto generate that.

What is the best way to create the requirements.txt ( or perhaps setup.py ) so that I can copy the application files to the raspberry pi and install all of the necessary dependencies.

Thanks very much for any insight and help you can provide.
10 comments
Comment actions Permalink
Official comment

Hi Bhavnaberi26! We have a similar feature request, please vote for PY-18847.

Comment actions Permalink
@TheYoungSoul, I dont believe that PyCharm will generate the requirements.txt file for you. The best way to do this is to get pip to do this for you.

pip freeze > requirements.txt
prettyPrint();
This effectively tells pip to freeze the packages that have been installed into the current environment and output them to stdout (from where they can be redirected to the requirements.txt file).

One thing to be aware of is that pip will list the packages that are installed in the environment where the active version of python is installed.

So if your project has it's own virtual environment (virtualenv) which is separate from the default Python install, and you open a shell and type pip freeze > requirements.txt, without first activating your virtual environment, then you will simply freeze the packages installed in the default Python installation and not the one that is specific to your Flask project.

Whilst PyCharm doesn't output a requirements.txt, if it finds a requirements.txt file in the project root, it will check to ensure that the package requirements are satisfied.

I hope that helps,

Bletch.
3
Comment actions Permalink
Thanks Bletch.

As a follow up.  I now understand that Pycharm does not directly help me with the generation of the requirements file - so I can stop trying.

I do have a virtual environment that I created when I created my PyCharm project, and I always keep my virtualenvs outside the project ( not sure if that matters ).  

In PyCharm I add modules ( like flask, script, requests, etc ) however, if I just open a shell and activate my virtualenv and type pip freeze just to see what it will return, all it returns is wsgiref==0.1.2 and not any of the modules I added via PyCharm.  PyCharm knows of them - the project works.  

does PyCharm keep its own virtualenv that I need to activate in the shell that is different than the one I thought I was creating when I created the project?

It does seem to me that PyCharm could make this easier since it has all of the info - but I am not complaining.  I love PyCharm.

Thanks again
0
Comment actions Permalink
@TheYoungSoul, it looks like you hadn't activated the correct virtual environment before typing pip freeze.

You can check the virtual environment that PyCharm is using for your project by visiting the Project Interpreter settings:

File -> Settings -> Project Interpreter

You'll see from the attached image that in my environment, the virtual environment for my project is in

~/coding/pythontop40/venv34

PyCharmInterpreterSettings.png

I'm running on Linux so the ~ at the beginning of the path means my home directory.

This means that for me to activate this virtual environment outside of PyCharm, I would need to type

Activating a Virtual Environment in Linux
cd ~/coding/pythontop40
. venv34/bin/activate 
prettyPrint();
Note that there is a space between the . and the start of the venv34/bin/activate.

Once this command has executed successfully, the $ command prompt should change to have the name of the virtual environment prepended to it - in my case this is (venv34).

If you are on Windows, then you would type something like:

Activating a Virtual Environment in Windows/DOS
cd \users\user\coding\pythontop40
venv34\Scripts\activate.bat
prettyPrint();
So, if I'm right and you are not activating the same virtual environment that PyCharm is using for your project, and you follow these steps, you should be able to type pip freeze to get the list of currently installed modules for this Python installation.

Result of running pip freeze
Jinja2==2.7.3
MarkupSafe==0.23
Pygments==2.0.1
Sphinx==1.2.3
arrow==0.4.4
booby==0.7.0
coverage==3.7.1
docutils==0.12
expects==0.6.0
flake8==2.2.5
mccabe==0.2.1
mock==1.0.1
munch==2.0.2
nap==2.0.0
nose==1.3.4
pep8==1.5.7
pkginfo==1.1
pyflakes==0.8.1
python-dateutil==2.2
requests==2.4.3
six==1.8.0
sphinx-rtd-theme==0.1.6
sphinxcontrib-napoleon==0.2.8
twine==1.3.1
prettyPrint();

Let me know how you go.
1
Comment actions Permalink

I am having the same difficulty.  Using python 3.4. Virtual env on windows 10.

When I launch the virtualenv and do a pip freeze, I only get the packages that were loaded globally.

I have activated the appropriate virtual environment.  It shows in the parentheses on the left.  Yet, the pip freeze is completely inconsistent with what is displayed in Settings.

0
Comment actions Permalink

I am also facing the same problem. Every time I add a new package, I have to freeze my packages. This is cumbersome. Is there any alternate so that I can get all the requirements without freezing my packages in Virtual Environment

1
Comment actions Permalink

I too want Pycharm to automatically create requirements.txt

2
Comment actions Permalink

I also would like Pycharm to create a requirements based on the active setup.

Beyond that... I would love it if Pycharm could setup a venv base one the current setup

1
Comment actions Permalink

for anyone still struggling with this there is a simple solution.

in your project view right click on the project root folder and click Open In Terminal

This opens the terminal and activates the Venv.

then 

pip freeze > requirements.txt

you can then close the terminal.

Its not an auto create method but its simple enough.

2
Comment actions Permalink

I found a way to generate the requirements.txt

install pipreqs --> pip install pipreqs

Then use pipreqs to generate requirments.txt file --> pipreqs <folderlocation>

0

Please sign in to leave a comment.