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.
1
14 comments
Official comment

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

Avatar
Permanently deleted user
@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

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.
8
Avatar
Permanently deleted user
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
Avatar
Permanently deleted user
@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 

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

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


Let me know how you go.
2

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
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

I too want Pycharm to automatically create requirements.txt

3
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

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.

4
Avatar
Permanently deleted user

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

EDIT: apparently this is a new feature that pre-dates this discussion. I'm going to start a new thread to discuss this. My original comment follows:

I'm also quite confused by this. I understand that I can use `pip freeze > requirements.txt` in my venv to generate the project requirements. This works well, and I've been doing it for years. However, in PyCharm's documentation, I see the following: https://www.jetbrains.com/help/pycharm/managing-dependencies.html

It seems like I should be able to use this menu to generate a requirements.txt file based on the installed packages. However, in my project it was initially greyed out. I was able to have the menu item activate after generating a blank `requirements.txt` file, but then selecting the menu item did nothing. The window shown in the above documentation never appears.

Is this feature just not supported? Is there something else I need to do before I can use it?

0

Hello Gavin, 

 

This is supported since PyCharm 2020.1.  "However, in my project it was initially greyed out." - could you please attach a screenshot? Do you get a popup message "add imported packages to requirements " right after the file is created manually?  

 

 

0

Hi Antonina,

After creating the `requirements.txt` file manually, the menu item activated in my project. Because of this I can't screenshot that behaviour, sadly.

I did not get the "add imported packages to requirements" popup after creating the file manually. However, I created a blank file, so maybe that didn't trigger it?

Also, I should mention that this is a new project, but it was initially created outside of PyCharm. I then opened it as a folder. That said, I just created a new project within PyCharm as a test, using a virtual env. The "Sync Python Requirements" menu item is available to begin with in this new project. I installed Django through "Preferences -> Project: (venv name) -> Python Interpreter", and then tried to sync the requirements in this new project. The popup opened, I chose to sync, but afterward I was just show a blank `requirements.txt` file in the editor.

EDIT: For reference, here is the about information for my install:

PyCharm 2020.1 (Professional Edition)
Build #PY-201.6668.115, built on April 7, 2020
Licensed to Gavin Anderegg
Subscription is active until March 30, 2021
Runtime version: 11.0.6+8-b765.25 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6
GC: ParNew, ConcurrentMarkSweep
Memory: 2014M
Cores: 4
Non-Bundled Plugins: com.jetbrains.intellij.datalore

0

Hello, 

 

First of all, please make sure there are any imports in the project's code. This file should contain only packages imported in a code. 

I have reproduced this on my side, and it appeared that once you click "OK" in "Sync Python Requirements" window at the first time, only blank file is being created, after clicking   "add imported packages to requirements" popup the file will be written with the list of packages.  And this is a bug, please vote for the ticket https://youtrack.jetbrains.com/issue/PY-41953 to be updated on a resolution. 

 

 

1

Please sign in to leave a comment.