How can I activate a virtualenv in Linux?

2025-03-05 09:17:00
admin
原创
94
摘要:问题描述:I have been searching and tried various alternatives without success and spent several days on it now; it is driving me mad.I am running on Red Hat Li...

问题描述:

I have been searching and tried various alternatives without success and spent several days on it now; it is driving me mad.

I am running on Red Hat Linux with Python 2.5.2.
I began using the most recent Virtualenv, but I could not activate it. I found somewhere suggesting I needed an earlier version, so I have used Virtualenv 1.6.4 as that should work with Python 2.6.

It seems to install the virtual environment ok

python virtualenv-1.6.4/virtualenv.py virtual

Output:

New python executable in virtual/bin/python
Installing setuptools............done.
Installing pip...............done.

The environment looks ok

cd virtual
dir

Output:

bin  include  lib

Trying to activate

. bin/activate

Output:

/bin/.: Permission denied.

I checked chmod

cd bin
ls -l

Output:

total 3160
 -rw-r--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
 -rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
 -rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
 -rw-r--r-

It was a problem, so I changed it

ls -l

Output:

total 3160
-rwxr--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r--    1 necrailk biz12        1005 Jan 30 11:38 activate_this.py
-rwxr-xr-x    1 necrailk biz

Tring activate again

. bin/activate

Output:

/bin/.: Permission denied.

Still no joy...


解决方案 1:

Here is my workflow after creating a folder and cd'ing into it:

virtualenv venv --distribute

Output:

New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.

And

source venv/bin/activate
python

解决方案 2:

You forgot to do source bin/activate, where source is an executable name. It struck me the first few times as well. It is easy to think that the manual is telling "execute this from root of the environment folder".

There isn't any need to make activate executable via chmod.

解决方案 3:

You can do

source ./python_env/bin/activate

Or just go to the directory:

cd /python_env/bin/

and then

source ./activate

解决方案 4:

Go to the project directory. In my case microblog is the flask project directory and under microblog directory there should be app and venv folders. then run the below command, This is one worked for me in Ubuntu.

source venv/bin/activate

enter image description here

解决方案 5:

cd to the environment path, and go to the bin folder.
At this point, when you use the ls command, you should see the "activate" file.

Now type

source activate

解决方案 6:

Run this code. It will get activated if you are on a Windows machine:

. venv/Scripts/activate

Run this code. It will get activated if you are on a Linux or Mac machine:

. venv/bin/activate

解决方案 7:

mkdir <YOURPROJECT>: Create a new project

cd <YOURPROJECT>: Change directory to that project

virtualenv <NEWVIRTUALENV>: Creating a new virtualenv

source <NEWVIRTUALENV>/bin/activate: Activating that new virtualenv

解决方案 8:

The problem there is the /bin/. command. That's really weird, since . should always be a link to the directory it's in. (Honestly, unless . is a strange alias or function, I don't even see how it's possible.) It's also a little unusual that your shell doesn't have a . builtin for source.

One quick fix would be to just run the virtualenv in a different shell. (An obvious second advantage being that instead of having to deactivate you can just exit.)

/bin/bash --rcfile bin/activate

If your shell supports it, you may also have the nonstandard source command, which should do the same thing as ., but may not exist. (All said, you should try to figure out why your environment is strange or it will cause you pain again in the future.)

By the way, you didn't need to chmod +x those files. Files only need to be executable if you want to execute them directly. In this case you're trying to launch them from ., so they don't need it.

解决方案 9:

Instead of ./activate, use source activate:

See this screenshot

解决方案 10:

For Windows, you can perform it as:

To create the virtual environment as: virtualenv envName –python=python.exe (if not, create an environment variable)

To activate the virtual environment: path oenvNameScriptsactivate

To deactivate the virtual environment: path oenvScriptsdeactivate

It works fine on the new Python version.

解决方案 11:

Windows 10

In Windows, these directories are created:

Windows 10 virtual environment directories

To activate the virtual environment in Windows 10.

downscriptsactivate

The \scripts directory contains the activate file.

Linux Ubuntu

In Ubuntu, these directories are created:

Linux Ubuntu virtual environment directories

To activate the virtual environment in Linux Ubuntu.

source ./bin/activate

The /bin directory contains the activate file.


The virtual environment can be copied from Windows to Linux Ubuntu and vice versa

If the virtual environment folder is copied from Windows to Linux Ubuntu then according to directories:

source ./down/Scripts/activate

解决方案 12:

I would recommend virtualenvwrapper as well. It works wonders for me and how I always have problems with activating.

解决方案 13:

On Mac, change the shell to Bash (keep note that virtual env works only in the Bash shell)

. venv/bin/activate

.: Command not found.

source venv/bin/activate

Badly placed ()'s.

bash
source venv/bin/activate

New prompt:

(venv) bash-3.2$

Bingo, it worked. See, the prompt changed.

On Ubuntu:

source toolsenv/bin/activate

New prompt:

(toolsenv) user@local_host~/tools$

Note: the prompt changed

解决方案 14:

I was getting this error "-sh: 13: source: not found"

Resolution is to first login as sudo

  1. $sudo su

Then execute the command

  1. #source virtual_env_name/bin/activate

解决方案 15:

Create your own Python virtual environment called <your environment _name>:.

I have given it the name "VE".

git clone https://github.com/pypa/virtualenv.git
python virtualenv.py VE

To activate your new virtual environment, run (notice it's not ./ here):

. VE/bin/activate

Sample output (note prompt changed):

(VE)c34299@a200dblr$

Once your virtual environment is set, you can remove the Virtualenv repository.

解决方案 16:

I had trouble getting source /bin/activate running, but then I realized I was using tcsh as my terminal shell instead of Bash.

Once I switched, I was able to activate venv.

解决方案 17:

I had faced the same problem. The main reason being that I created the virtualenv as a "root" user.
But later I was trying to activate it using another user.

chmod won't work as you're not the owner of the file, hence the alternative is to use chown (to change the ownership).

For example, if you have your virtualenv created at /home/abc/ENV.

Then cd to /home/abc.

And run the command: chown -Rv [user-to-whom-you want-change-ownership] [folder/filename whose ownership needs to be changed]

In this example, the commands would be: chown -Rv abc ENV

After the ownership is successfully changed, you can simply run source /ENV/bin/./activate and your should be able to activate the virtualenv correctly.

解决方案 18:

  1. Open a PowerShell window and navigate to your application folder

  2. Enter your virtualenv folder, for example: cd . envScripts

  3. Activate the virtual environment by typing .activate

解决方案 19:

The eval command is used because the regular source or . methods might not work reliably in some container environments. This approach executes the activation script's contents directly

  
eval "$(cat /app/venv/bin/activate)"

解决方案 20:

This is for Git Bash running on Windows Server 2022. There is a question for Git Bash but it links to here as a duplicate.

Everything works fine after . my-venv/Scripts/activate if I stay on the drive with the venv folder. But if I try to work on another drive it falls apart:

$ which python
/f/my-venv/Scripts/python
$ cd /c
$ which python
/c/Users/myuser/AppData/Local/Programs/Python/Python311/python

Not sure if this is a known problem or something specific to my system, but in case somebody else runs into it, there it is...

(I worked around it by keeping code & venv on the same drive).

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3970  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2740  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   79  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   87  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   74  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用