Answers: <p>I'm guessing you created and activated in your terminal?</p>
  <p>You need to tell vscode about the venv. It's pretty good at finding and using them but sometimes it dectects the wrong one.</p>
  <p>At the bottom of the vscode window you should see the environment listed. If its the wrong one click it and find the right one.</p>
  <p><a href="https://i.sstatic.net/EqduX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EqduX.png" alt="The python venv selector in vscode task bar" /></a></p>
  <p>Also your <code>launch.json</code> and <code>setup</code> file seems wrong.</p>
  <p>The <code>program</code> should probably be <code>script</code> or <code>python3 ${file}</code> in <code>launch.json</code>.</p>
  <p>In your setup file <code>scripts</code> should be <code>scripts=bin/my-cool-program</code>. Then you can change <code>program</code> in <code>launch.json</code> to <code>my-cool-program</code>. You should also be able to run it from your terminal like <code>$ my-cool-program -q data</code>.</p>
  <p>This is a great way to learn, but it's not how I would create a CLI program today in python. I think you would be better off using <code>poetry</code> and <code>click</code>. Here is an <a href="https://cjolowicz.github.io/posts/hypermodern-python-01-setup/" rel="nofollow noreferrer">excellent guide</a> to &quot;modern&quot; python.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-08-02T13:28:12.477Z
Answers: 2022-08-02T13:28:12.477Z
Answers: Levi
Body: <p>I asked this question on the regular stackoverflow but i think you guys will be much more competent about this topic. My guess is that is some sort of python setup issue.</p>
  <p>When I write an import line like this <code>from mypackage import something</code> and run it using vscode tools it gives my an error, when I run it using venv python it works. Same for pytest, I have to run it like this: <code>python3 -m pytest tests</code> for it to work. The main thing that i'm trying to accomplish is to be able to run/debug stuff from vscode.</p>
  <p>The steps I took for it to work:</p>
  <ul>
  <li>Create venv &amp; avtivate it</li>
  <li>Turned my project into a package
  <ul>
  <li>Create setup.py</li>
  <li>pip3 install -e .</li>
  </ul>
  </li>
  <li>Create a launch.json file</li>
  </ul>
  <p>The launch.json file:</p>
  <pre><code>{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
      {
        &quot;name&quot;: &quot;Python: Current File&quot;,
        &quot;type&quot;: &quot;python&quot;,
        &quot;request&quot;: &quot;launch&quot;,
        &quot;program&quot;: &quot;${file}&quot;,
        &quot;console&quot;: &quot;integratedTerminal&quot;,
        &quot;args&quot;: [&quot;-q&quot;, &quot;data&quot;]
      }
    ]
  }
  </code></pre>
  <p>And the setup file:</p>
  <pre><code>from setuptools import setup
  
  setup(
    name='mypackage',
    version='0.1.0',
    packages=['mypackage'],
    scripts=['bin/script'],
    license='LICENSE.txt',
  
  )
  </code></pre>
  <p>Expected behaviour:</p>
  <ul>
  <li>pytest not to give an import error</li>
  <li>vscode not to give an import error</li>
  </ul>
  <p>What I get:</p>
  <ul>
  <li>works only when I explicitly run it like this: <code>python3 main.py</code></li>
  </ul>
  <p>Trying to solve it for a solid hour. Many many many thanks for helping!</p>
  
Tags: <python>
Title: Vscode/pytest gives me an error when importing
UserDisplayName: Ruslan Yakushev
ThreadText: # Vscode/pytest gives me an error when importing
  
  (Asked by: username_0 on 2022-08-02)
  
  I asked this question on the regular stackoverflow but i think you guys will be much more competent about this topic. My guess is that is some sort of python setup issue.
  
  When I write an import line like this `from mypackage import something` and run it using vscode tools it gives my an error, when I run it using venv python it works. Same for pytest, I have to run it like this: `python3 -m pytest tests` for it to work. The main thing that i'm trying to accomplish is to be able to run/debug stuff from vscode.
  
  The steps I took for it to work:
  
  * Create venv & avtivate it
  * Turned my project into a package
    + Create setup.py
    + pip3 install -e .
  * Create a launch.json file
  
  The launch.json file:
  
  ```
  {
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "args": ["-q", "data"]
      }
    ]
  }
  
  ```
  
  And the setup file:
  
  ```
  from setuptools import setup
  
  setup(
    name='mypackage',
    version='0.1.0',
    packages=['mypackage'],
    scripts=['bin/script'],
    license='LICENSE.txt',
  
  )
  
  ```
  
  Expected behaviour:
  
  * pytest not to give an import error
  * vscode not to give an import error
  
  What I get:
  
  * works only when I explicitly run it like this: `python3 main.py`
  
  Trying to solve it for a solid hour. Many many many thanks for helping!
  
  **Tags:** python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-02. Score: 0
  
  I'm guessing you created and activated in your terminal?
  
  You need to tell vscode about the venv. It's pretty good at finding and using them but sometimes it dectects the wrong one.
  
  At the bottom of the vscode window you should see the environment listed. If its the wrong one click it and find the right one.
  
  [![The python venv selector in vscode task bar](https://i.sstatic.net/EqduX.png)](https://i.sstatic.net/EqduX.png)
  
  Also your `launch.json` and `setup` file seems wrong.
  
  The `program` should probably be `script` or `python3 ${file}` in `launch.json`.
  
  In your setup file `scripts` should be `scripts=bin/my-cool-program`. Then you can change `program` in `launch.json` to `my-cool-program`. You should also be able to run it from your terminal like `$ my-cool-program -q data`.
  
  This is a great way to learn, but it's not how I would create a CLI program today in python. I think you would be better off using `poetry` and `click`. Here is an [excellent guide](https://cjolowicz.github.io/posts/hypermodern-python-01-setup/) to "modern" python.
  
  
  ---
  
  