简体   繁体   English

如何使用 Python 激活虚拟环境

[英]How to activate a virtual environment using Python

I am struggling to activate my virtual environment located here C:\Users\HP\project1_env .我正在努力激活位于C:\Users\HP\project1_env的虚拟环境。

I have tried the following commands -我尝试了以下命令 -

Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\HP>project1_env\scripts\activate

C:\Users\HP>project1_env\scripts\activate.bat

C:\Users\HP>cd project1_env\scripts\activate
The directory name is invalid.

C:\Users\HP>cd C:\Users\HP\project1_env\Scripts

C:\Users\HP\project1_env\Scripts>activate

C:\Users\HP\project1_env\Scripts>cd C:\Users\HP\project1_env\Scripts\activate
The directory name is invalid.

C:\Users\HP\project1_env\Scripts>C:\Users\HP\project1_env\Scripts\activate.bat

Does anyone have any other suggestions?有没有人有任何其他建议?

Update更新

Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\HP>project1_env\scripts\activate

C:\Users\HP>project1_env\scripts\activate

C:\Users\HP>python manage.py runserver
python: can't open file 'manage.py': [Errno 2] No such file or directory

C:\Users\HP>python -c "import sys; print(sys.executable)"
C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe

C:\Users\HP>

You should run python manage.py runserver in the directory of your Django project, not where your virtual environment is placed.您应该在 Django 项目的目录中运行python manage.py runserver ,而不是放置虚拟环境的位置。 Try changing directory to where your manage.py file is placed and try again.尝试将目录更改为放置 manage.py 文件的位置,然后重试。

Also, to activate your virtual environment, you should run this command: %PATH_TO_YOUR_VENV_DIRECTORY\Scripts\activate此外,要激活您的虚拟环境,您应该运行以下命令: %PATH_TO_YOUR_VENV_DIRECTORY\Scripts\activate

I hope this answers your question.我希望这回答了你的问题。

I consider that your project is located in C:\Users\HP and from there you should run project1_env\Scripts\activate and then move to where your manage.py file is located and run我认为您的项目位于C:\Users\HP并从那里您应该运行project1_env\Scripts\activate然后移动到您的 manage.py 文件所在的位置并运行
python manage.py runserver if you didn't activate your virtual environment you will get an error saying Django module is not found error . python manage.py runserver如果你没有激活你的虚拟环境你会得到一个错误,说Django module is not found error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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