I have build my virtual env with this command:
virtualenv env --distribute --no-site-packages
And then I have installed several modules (django etc) into env with pip, the problem is that when I wanted to run the code on the second machine it would not work, here is what I have done:
visgean#rewitaqia:~/scripty/project_name$ source ./env/bin/activate
(env)visgean#rewitaqia:~/scripty/project_name$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__file__
'/home/visgean/scripty/project_name/env/lib/python2.7/site-packages/django/__init__.pyc'
but when I want to run it on the second machine:
(env)user#debian:~/project_name$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>>
I wild error appears! The first machine is ubuntu, the second one is ubuntu. There seems to be some broken links in /home/user/project_name/env/lib/python2.7 , is that the problem? and if so, how should I prevent it/repair it?
If you are just copying the virtualenv to the second machine you may encounter some issues. From the virtualenv site:
Normally environments are tied to a specific path. That means that you
cannot move an environment around or copy it to another computer. You
can fix up an environment to make it relocatable with the command:
$ virtualenv --relocatable ENV
This will make some of the files
created by setuptools or distribute use relative paths, and will
change all the scripts to use activate_this.py instead of using the
location of the Python interpreter to select the environment.
Note: you must run this after you've installed any packages into the
environment. If you make an environment relocatable, then install a
new package, you must run virtualenv --relocatable again.
I have just noticed that I did have a wrong version of python on the second machine - debian does not have python2.7, installing 2.7 and pip for is the solution
Related
I have created a virtual environment in python in ubuntu in Raspberry Pi with:
python -m venv ./venv/myname
That gives me folderstructure:
myFolder
+-- venv
+-- myApplication.py
Im activating my virtualenvironment running:
source venv/myname/bin/activate
What I dont understand is when I am running
python myApplication.py
I can run the application using python packages installed globally but not in my virtual environment. For example I can import numpy without having it installed in my virtual environment but globaly. I thought I needed to install everything within my virtual environment no matter if I have it globally or not. Do I misunderstand something here?
Im using python 3.7
This is my output from print(sys.path)
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685-1.0.1-py3.7.egg', '/usr/lib/python3/dist-packages']
>>>
UPDATE
The virtual environment seems to be running after all with testing it with os.environ['VIRTUAL_ENV']. Weird enough I can run packages I have not installed in my virtual environment. Here is output from my terminal:
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ python
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
'/home/pi/Desktop/myproject/myenv'
>>> exit()
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ deactivate
pi#raspberrypi:~/Desktop/myproject/myenv$ pythonPython 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/os.py", line 678, in __getitem__
raise KeyError(key) from None
KeyError: 'VIRTUAL_ENV'
>>>
It is very clear to me your virtual environment (venv) has not been activated (see this SO post for ways to verify).
You can run your programs without activating by running ./venv/myname/bin/python myApplication.py. However, you probably don't want to be using this everytime as it's verbose.
There can be a ton of system-specific reasons why your venv isn't being activated after running source. My recommendation is to first reinstall virtualenv, delete your venv, and recreate.
I have installed the latest version of python (3.7.3)
When I go to cmd and I put py, it says my version is 3.7.3. But the problem comes when i put python or python --version, because it says that my version is 2.7.10. And i don´t know why.
I´m learning how to do my first website, and i´m just following what the tutorial says. I don´t know if the version of python is the cause of the problem, but i tell you what happens to me appart from not knowing what version i have.
I´m using Visual Studio Code and the guy from the tutorial said that we had to have at least a version of python over 3, and to find this out we have to write on CMD python --version. Then we had to know what version of pip we have putting pip --version.
Now I´ll show you the code he did and I copied, I had to install flask from CMD putting pip install flask.
So when I go to CMD to run this as the youtuber did, putting cd Desktop then cd NoFear (NoFear is the name of the folder) and finally python index.py. I got the following output:
C:\Users\Usuario\Desktop\NoFear>python index.py
Traceback (most recent call last):
File "index.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
C:\Users\Usuario>py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python --version
Python 2.7.10
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hello World'
if __name__=='__main__':
app.run()
Type Py -3 index.py in cmd instead of typing python index.py. I hope this helps
You have two versions of python so uninstall the older version first
You can create a virtual environment that will isolate your Python dependencies. Because you have stated you are using Python 3.7.3, you can create a virtual environment by running the following command:
$ python3 -m venv .venv
Then you can activate it by using:
$ source .venv/bin/activate
Then if you run python in the terminal, you will start Python 3.7.3 because your virtual environment has been created using this version of Python.
Finally, if you want to deactivate the virtual environment just run:
$ deactivate
You can uninstall a your other python versions or if you want to keep them you can rearrange the path. That being said I would recommend looking into creating virtual environments.
1) In the bottom left of windows search for: environment variables
2) Select Environment Variables... on the bottom right.
3) Select the Path variable on the top (or bottom if there is not one on the top).
4) Move python3 above python2.7 and save.
Maybe get atom cause you are working in IDLE and i had the same problem each time you want to use code form IDLE on other software you cannot run it because of the lines on top
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
you have to take them of or use virtual studio or atom or oder IDE, i think.
While trying to install atom-lint package in Atom editor I somehow corrupted my conda installation.
I did the following things that might have caused the issue:
Installed a python dependency Flake8 using conda install.
Messed around with Atom Init Script (I can provide more info if needed)
After I did these things I encountered the following problem:
If I run conda I get
$ conda
Traceback (most recent call last):
File "/Users/me/miniconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Strangely enough if I run python in my command line anaconda still seems to be installed an working.
$ python
Python 3.7.1 (default, Oct 23 2018, 14:07:42)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
I have also noticed that the Python3 bin has disappeared from the conda environment folder ~/miniconda3/envs/my_env/bin/
This is very strange and I thought might be related.
Any help would be much appreciated.
I ended up making a backup copy of the miniconda3/envs folder, reinstalling miniconda and copying the environment back in. It works now, not sure what caused the issue.
I'm trying to use virtualenv in my new mainly Python project. The code files are located at ~/Documents/Project, and I installed a virtual environment in there, located at ~/Documents/Project/env. I have all my packages and libraries I wanted in the env/bin folder.
The question is, how do I actually run my Python scripts, using this virtual environment? I activate it in Terminal, then open idle as a test, and try
"import django"
but it doesn't work. Basically, how can I use the libraries install in the virtual environment with my project when I run it, instead of it using the standard directories for installed Python libraries?
Check out the example below, and also the virtualenv documentation. It's actually fairly straightforward if you follow the steps:
virtualenv Project # creates a new Project dir
cd Project/bin # could just call Project/bin
. activate # should now have (Project) in the prompt name
pip install django # without this, won't be able to import django
deactivate # switch of virtual mode
I tried the above out in my Mac and worked fine. Here's a transcript for reference.
Transcript of operations
[MacMini]<Documents> :virtualenv Project
[MacMini]<Project> :cd bin/
[MacMini]<bin> :python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> quit()
[MacMini]<bin> :. activate
(Project)[MacMini]<bin> :pip install django
You are using pip version 6.0.6, however version 8.1.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting django
Downloading Django-1.9.4-py2.py3-none-any.whl (6.6MB)
100% |################################| 6.6MB 1.2MB/s
Installing collected packages: django
Successfully installed django-1.9.4
(Project)[MacMini]<bin> :python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> quit()
(Project)[MacMini]<bin> :deactivate
It's also good practice to make a requires.txt file for all your dependencies. If for example your project requires Flask and pymongo, create a file with:
Flask==<version number you want here>
pymongo==<version number you want here>
Then you can install all the necessary libraries by doing:
pip install -r requires.txt
Great if you want to share your project or don't want to remember every library you need in your virtualenv.
I created a virtualenv, and while it has many system paths, it doesn't have others. Specifically, pyshared and dist-packages don't seem to be included. As a result, my system-wide MySQLdb and psycopg2 aren't available. Any ideas why?
Seems to be related to Ubuntu's messing with python and virtualenv
The only possible way that i'm aware of, is if you have created your virtualenv with the argument --no-site-packages:
from Here:
If you build with virtualenv
--no-site-packages ENV it will not inherit any packages from
/usr/lib/python2.5/site-packages (or
wherever your global site-packages
directory is). This can be used if you
don't have control over site-packages
and don't want to depend on the
packages there, or you just want more
isolation from the global system.
so Here is an example to understand more:
First i will create a virtualenv normally (without --no-site-package) and you will see that
i can always access django that is installed in my system site-packages (or dist-packages):
$ virtualenv A
New python executable in A/bin/python
Installing setuptools............done
$ source A/bin/activate
(A)$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__file__
'/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'
But now i will create the virtual env using --no-site-package:
$ virtualenv B --no-site-package
New python executable in B/bin/python
Installing setuptools............done.
$ source B/bin/activate
(B)$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
now you see that virtaulenv was able to access django from system dist-packages (ubuntu) in my machine.
Hope this will help :)