Python script can't find virtualenv path - python-2.7

Basically, a python script I'm running doesn't find the path to a script located in a virtualenv folder, while I can see that script from bash.
Details:
I have Python 2.7 installed globally and OpenCV3.1 installed in virtualenv, with virtualenv located at the path
~/.virtualenvs/cvcorrect
I'm trying to run a script (written by someone else) that requires loading of activate_this.py to run the script. In my case, it is located at
~/.virtualenvs/cvcorrect/bin/activate_this.py
I can see that it is there it when I look into that folder from shell. However, when I run the script, with the correct path, it gives the common
IOError: [Errno 2] No such file or directory:
'~/.virtualenvs/cvcorrect/bin/activate_this.py'
Tried running this both as regular and superuser. Same results. Running in Ubuntu 64 bit in VMWare 15.

The solution was to find and input the full path: /root/.virtualenvs/cvcorrect/bin/activate_this.py
This was not as straightforward as usual, because .virtualenvs folder was located in the root folder which was not accessible using GUI. Also, bash shell gave me the path
~/.virtualenvs/cvcorrect/bin/activate_this.py
The solution was to cd to the folder from shell and use:
readlink -f activate_this.py
Which finally gave me the correct path.

Related

Virtualenv and django not working in bash on windows 10

I have a problem with using virtualenv and django in bash. If I type python -m venv env in cmd, then env\Scripts\activate, and then virtualenv - I get 'virtualenv' is not recognized as an internal or external command, operable program or batch file.. If I do the same in bash I get bash: virtualenv: command not found. How do I fix this?
Try the following to resolve your issue.
Check all of the environment variables related to the software you require to be used at least.
Check the permissions for files and folders for the software.
Sometimes uninstalling and installing the software with issues can solve problems quickly.
If you have performed number 2. and you are still have errors, proceed to number 3.
You may have dependencies missing, a good tool i have used on Windows is Dependency Walker, and the software will check if any file and dependencies are missing, and you should be able to download them.
An error message may output a file is not found but in fact a dependency is missing, relating to the software you are trying to run.
Try the following steps in the terminal, it may solve your problem.
using terminal, mkdir to make a directory for your project
cd to your project folder/dir
type pip3 freeze, it will show up all the installed packages and dependencies on global scope/system
but we gonna have a venv where we will install our necessary packages and dependencies
type python3 -m venv ./venv to create venv inside your current project folder, please ensure you are inside the folder before running this command
[if you are not using python 3, then the command will be python -m venv ./venv]
to actiavte environment,
on mac, run source ./venv/bin/activate ||
on windows, run .\venv\Scripts\activate.bat [if it doesn't work, try to put your absolute path]
you can check what is installed inside venv using pip freeze, you will see nothing inside the venv
Now you can install django inside venv for your project
to deactivate the environment, just type deactivate

'gcloud init' command returns "Permission denied"

I've installed Google Cloud SDK on my laptop. The gcloud command is available/accessible via git bash from any subdirectory. However, the gcloud init command returns the following error:
/c/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin/gcloud:
line 191: /c/Users/me/AppData/Local/Microsoft/WindowsApps/python:
Permission denied /c/Program Files (x86)/Google/Cloud
SDK/google-cloud-sdk/bin/gcloud: line 191:
/c/Users/me/AppData/Local/Microsoft/WindowsApps/python: Permission
denied
Any idea what the issue might be or how to debug it?
Hmm, so your shell is bash? And you're on Windows apparently, so try using a cmd shell (at the start menu, hunt for cmd and run it). See if you can run python from there. If that works, then running gcloudshould probably work too.
If that doesn't work, then you've probably got python installed improperly somehow. If you didn't install it, maybe something else installed it for you as part of a package. You should probably uninstall and reinstall it (https://www.python.org/downloads/). Also check if gcloud requires any specific version of python first.
Include location of python in your path.
If it looks for some other name of python e.g. python3, include soft link to python
prompt> ln -s python python3

Error message 'mkvirtualenv is not recognized as an internal or external command'

While I was trying to execute the mkvirtualenv command on the command prompt, I was getting this error:
C:\Users\mukesh>mkvirtualenv myproject
'mkvirtualenv' is not recognized as an internal or external command, operable program or batch file.
For Python 3.3 or newer, Commands for installing, creating and activate virtual environment has been changed.
You can install virtual environment using pip:
py -m pip install --user virtualenv
For creating new environment:
py -m venv myproject
To activate your virtual environment:
.\myproject\Scripts\activate
After activating virtual environment, You’ll see “(myproject)” next to the command prompt.
You may find this link useful, as it shows the steps required. It is possible you have simply missed the earlier steps, leading to the error.
The below information is from: https://docs.djangoproject.com/en/2.2/howto/windows/
This will run you through the creation of a virtual environment on Windows:
Install virtualenv and virtualenvwrapper¶
virtualenv and virtualenvwrapper provide a dedicated environment for each Django project you create. While not mandatory, this is considered a best practice and will save you time in the future when you’re ready to deploy your project. Simply type:
pip install virtualenvwrapper-win
Then create a virtual environment for your project:
mkvirtualenv myproject
The virtual environment will be activated automatically, and you’ll see “(myproject)” next to the command prompt to designate that. If you start a new command prompt, you’ll need to activate the environment again using:
workon myproject
To create a virtual environment,
decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.
A common directory location for a virtual environment is .venv. This name keeps the directory typically hidden in your shell and thus out of the way while giving it a name that explains why the directory exists. It also prevents clashing with .env environment variable definition files that some tooling supports.
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
some times the environment will not active because users are not allowed to run scripts in the current system so you should
-go to windows PowerShell
-run as administrator
-then past this code
-Set-ExecutionPolicy -Scope CurrentUser Unrestricted
now you are allowed to run scripts on your system
then try this again
py -m pip install --user virtualenv
For creating new environment:
py -m venv myproject
To activate your virtual environment:
.\myproject\Scripts\activat
If you have a Windows computer (and installed the Windows version virtualenvwrapper-win), make sure you add the Scripts folder to the path. As per the installation instructions:
To use these scripts from any directory, make sure the Scripts
subdirectory of Python is in your PATH. For example, if python is
installed in C:\Python27, you should make sure C:\Python27\Scripts is
in your PATH.
Here's some decent instructions on how to edit your path. Nowadays you can probably create a new entry after selecting to edit the path environment variable. That new entry should just be the location of the Scripts folder (including the Scripts folder). No need to add semicolons to a super long path name - it generally does that for you nowadays. You'll probably have to restart your computer for it to take effect.
You can find out where your Python is installed here. If you are on Windows and installed Python via the Microsoft Store, you won't see a Scripts folder. In that case, install Python from the Python website, not from the Microsoft Store.

unable to complete django installation

I downloaded the necessary files from the link https://github.com/django/django.git and pasted them in to my virtual env directory
After setting up and activating the virtualenv, when I run the following command:
$ pip install -e django/
It produces this error:
(ENV) C:\WINDOWS\system32>pip install -e django/ django/ should either
be a path to a local project or a VCS url beginning with svn+, git+,
hg+, or bzr+
I am a Windows user. I think the command is for bash not for cmd.
Is it necessary to use this git tool to finally work with django ?
As instructed on the Django website :
If you're just starting out with Django development I'd recommend looking at some YouTube videos before jumping into the Django docs. Personally when I was starting out I found that the docs were quite hard to understand in the beginning, but as you get better you can refer back to them more and more.
Here's a good beginner video series to get you started.
In any case, I would recommend using virtualenvwrapper-win so that you can work on multiple Django projects without any conflicts.
First, ensure that you have added Python to the Windows environment. Open CMD and run pip install virtualenvwrapper-win.
Then cd to whichever directory your project files will be in and run mkvirtualenv projectname.
Finally run setprojectdir path/to/folder
Now whenever you want to enter that virtual environment and work on your project all you have to do is run the command workon projectname and it'll do the rest for you. You'll know it worked if on each new line in the command prompt it gives you (projectname) in brackets.
To actually install Django all you need to run is pip install django while in the virtual environment.
From your question, I suppose that you are trying to install django inside your virtual directory. If that is correct you dont need to get it from git.
Alternate way is to create a directory "main" and then project directory "mydjangoproject" inside it and a virtual environment "env".
C:\>mkdir main
C:\>cd main
C:\main>mkdir mydjangoproject
C:\main>virtualenv env
Now activate the virtual environment.
C:\main>env\Scripts\activate
Then install all the package in it. e.g
(env) C:\main>pip install django

Trouble install PIP Windows

I have read posts on here but something doesn't seem to work. I am new to Python dev. I am running windows 7 64bit.
I am trying to install pip which I have downloaded and have easy_install in my c:\python27\scripts directory. When I use powershell and type
"python easy_install pip"
I get the error message:
"can't open file easy_install": [Errno 2] No such file or directory.
This seems odd as I can see the easy_install.exe is in that directory.
I have added c:\python27\scripts to my system PATH but, to be honest, I am not sure if this is right.
Could someone please help?
Thank you.
The selected answer dint fix it
This did:
python -m pip install -U pip
The best way to fix this is to compile easy_install yourself. First download the source code, and then compile it using python.
1 - Go here.
2 - Then extract the file, using 7-zip, or any software that can unzip a tar file. This is a tar.gz file, so after one extraction, it will give you the tar file, and then it will give you the actual directory and files for setup tools
3 - Go into the directory where you stored the contents of the extraction (using command prompt). For example, if you stored the directory on your desktop, you cd Desktop and then you go
cd dir_where_easy_install_is.
4 - Then run this command python setup.py install
That will install easy_install for you.
5 - Go back to your command line again, and then just type in easy_install pip, and that will install pip.
Then go ahead, and test it out, by installing django, just go into your command prompt, and enter this pip install django. After the download and the install, type in pip list, and see if django is in there. If it is, then pip has been successfully installed.
This worked running python from it's main dir against the easy_install.py in the extracted setuptools directory.
Then pip files should be in the Scripts dir.
Don't forget to run your CMD as administrator.