简体   繁体   中英

How to create a virtual environment for Django in Pycharm?

Currently, I am developing a Django web app in a virtual environment but when I try to edit the web app it just seems that the libraries that I installed in the virtual environment are not found. This is the scenario I have:

  1. I installed a virtual environment in C:\\Users\\myUser\\PycharmProjects .

     mkdir virtualenvs ; cd virtualenvs python -m venv virtualenvs 
  2. I got access to the new folder, activated the virtual environment, upgrading pip and started installing the libraries I need for my project.

     cd C:\\Users\\myUser\\PycharmProjects\\virtualenvs\\virtualenvs\\Scripts ; .\\activate (virtualenvs) python -m pip install --upgrade pip (virtualenvs) pip install django djangorestframework pandas numpy requests 
  3. I go back to C:\\Users\\myUser\\PycharmProjects to create my new Django project (I am still inside of my virtualenvs).

     (virtualenvs) mkdir C:\\Users\\myUser\\PycharmProjects\\apps ; cd C:\\Users\\myUser\\PycharmProjects\\apps (virtualenvs) django-admin.exe startproject app1 (virtualenvs) python manage.py startapp web_app_navigator (virtualenvs) python manage.py runserver 
  4. I am able to see the Django successful message The install worked successfully! Congratulations! The install worked successfully! Congratulations! , but when I open this new application in PyCharm to do some changes such as trying to import any library that I installed I get an error message that the library is not found.

My guess is that the virtualenvs and app folder is in some way disconnected from the Pycharm editor, so this might be the reason why PyCharm cannot find the libraries I installed. The easy solution should be to avoid using the virtual environment and just download the libraries but I am against this idea because I want to maintain each Django web app separated with its own libraries.

Did I miss any step such as connecting the app folder with the virtual environment folder? How can I make the virtual environment to work correctly in Pycharm? Any comment, suggestion or improvement to this question is greatly appreciated.

You can follow these steps from my personal website.

How to add virtual environment inside PyCharm on windows to run django project?

virtualenv tool comes bundled with PyCharm, You just need to install it by following these steps.

Steps to Create Virtual Environment

  1. Open the Settings dialog box, by going on left and clicking on file and going down the dropdown and select Settings or alternatively hit Ctrl+Alt+S .

  2. Click on your project on left and then click on Project Interpreter this opens a window on right.

  3. Now just choose Create VirtualEnv from dropdown in Project Interpreter if it is not there then click small button on right and select Create VirtualEnv . CAVEAT: In case this option does not appear, click on existing environment, select the Interpreter option and click on the ... button to look for your virtual environment setup. In your case, you should select C:\\Users\\myUser\\PycharmProjects\\virtualenvs\\virtualenvs\\Scripts\\python.exe . Remember that the Scripts folder contains all the libraries that have been downloaded when you created the virtual environment. Select apply and hit OK, so now you should be able to see all your libraries that were downloaded.

  4. Now in the Create Virtual Environment Window , give Name to your VirtualEnv choose Location to install and choose Base Interpreter from dropdown or by clicking button on right, Base Interpreter is the Python Interpreter.

  5. Click Ok and your new VirtualEnv is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM